deno.land / x / masx200_leetcode_test@10.6.5 / construct-string-from-binary-tree / index.ts
12345678910111213141516171819202122import { TreeNode } from "../binary-tree-inorder-traversal/TreeNode.ts";
export default function tree2str(root: TreeNode | null): string { if (!root) { return ""; } if (!root.left && !root.right) { return String(root.val); } if (!root.right) { return String(root.val) + "(" + tree2str(root.left) + ")"; } return ( String(root.val) + "(" + tree2str(root.left) + ")(" + tree2str(root.right) + ")" );}
Version Info