|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[606. 根据二叉树创建字符串](https://leetcode-cn.com/problems/construct-string-from-binary-tree/solution/by-ac_oier-i2sk/)** ,难度为 **简单**。 |
| 4 | + |
| 5 | +Tag : 「二叉树」、「DFS」、「递归」、「迭代」、「栈」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。 |
| 10 | + |
| 11 | +空节点则用一对空括号 `"()"` 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。 |
| 12 | + |
| 13 | +示例 1: |
| 14 | +``` |
| 15 | +输入: 二叉树: [1,2,3,4] |
| 16 | + 1 |
| 17 | + / \ |
| 18 | + 2 3 |
| 19 | + / |
| 20 | + 4 |
| 21 | +
|
| 22 | +输出: "1(2(4))(3)" |
| 23 | +
|
| 24 | +解释: 原本将是“1(2(4)())(3())”, |
| 25 | +在你省略所有不必要的空括号对之后, |
| 26 | +它将是“1(2(4))(3)”。 |
| 27 | +``` |
| 28 | +示例 2: |
| 29 | +``` |
| 30 | +输入: 二叉树: [1,2,3,null,4] |
| 31 | + 1 |
| 32 | + / \ |
| 33 | + 2 3 |
| 34 | + \ |
| 35 | + 4 |
| 36 | +
|
| 37 | +输出: "1(2()(4))(3)" |
| 38 | +
|
| 39 | +解释: 和第一个示例相似, |
| 40 | +除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。 |
| 41 | +``` |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +### 递归 |
| 46 | + |
| 47 | +生成字符串的规则其实就是在「前序遍历」输出节点值的同时,在每颗子树的左右添加一对 `()`(根节点除外),同时需要忽略掉一些不必要的 `()` 。 |
| 48 | + |
| 49 | +所谓的不必要就是指当以某个节点 $x$ 为根时,其只「有左子树」而「没有右子树」时,右子树的 `()` 可被忽略,或者「左右子树都没有」时,两者的 `()` 可被忽略。 |
| 50 | + |
| 51 | +或者反过来说,如果对于每个非空节点才添加 `()` 的话,那么当「有右子树」同时「没有左子树」时,左子树的 `()` 不能被忽略,需要额外添加,从而确保生成出来的字符串能够与「有左子树」同时「没有右子树」的情况区分开来,而不会产生二义性。 |
| 52 | + |
| 53 | +代码: |
| 54 | +```Java |
| 55 | +class Solution { |
| 56 | + StringBuilder sb = new StringBuilder(); |
| 57 | + public String tree2str(TreeNode root) { |
| 58 | + dfs(root); |
| 59 | + return sb.substring(1, sb.length() - 1); |
| 60 | + } |
| 61 | + void dfs(TreeNode root) { |
| 62 | + sb.append("("); |
| 63 | + sb.append(root.val); |
| 64 | + if (root.left != null) dfs(root.left); |
| 65 | + else if (root.right != null) sb.append("()"); |
| 66 | + if (root.right != null) dfs(root.right); |
| 67 | + sb.append(")"); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | +* 时间复杂度:令点数为 $n$,边数为 $m$。整体复杂度为 $O(n + m)$ |
| 72 | +* 空间复杂度:$O(n)$ |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +### 迭代(非递归) |
| 77 | + |
| 78 | +自然也能使用「迭代(非递归)」来做,使用栈来模拟上述递归过程。 |
| 79 | + |
| 80 | +由于当以某个节点 $x$ 为根节点时,其需要在 **开始** 前序遍历当前子树时添加 `(`,在 **结束** 前序遍历时添加 `)`,因此某个节点需要出入队两次。 |
| 81 | + |
| 82 | +同时区分是首次出队(开始前序遍历)还是二次出队(结束前序遍历),这需要使用一个 `set` 来做记录,其余逻辑与「递归」做法类似。 |
| 83 | + |
| 84 | +代码: |
| 85 | +```Java |
| 86 | +class Solution { |
| 87 | + public String tree2str(TreeNode root) { |
| 88 | + StringBuilder sb = new StringBuilder(); |
| 89 | + Set<TreeNode> vis = new HashSet<>(); |
| 90 | + Deque<TreeNode> d = new ArrayDeque<>(); |
| 91 | + d.addLast(root); |
| 92 | + while (!d.isEmpty()) { |
| 93 | + TreeNode t = d.pollLast(); |
| 94 | + if (vis.contains(t)) { |
| 95 | + sb.append(")"); |
| 96 | + } else { |
| 97 | + d.addLast(t); |
| 98 | + sb.append("("); |
| 99 | + sb.append(t.val); |
| 100 | + if (t.right != null) d.addLast(t.right); |
| 101 | + if (t.left != null) d.addLast(t.left); |
| 102 | + else if (t.right != null) sb.append("()"); |
| 103 | + vis.add(t); |
| 104 | + } |
| 105 | + } |
| 106 | + return sb.substring(1, sb.length() - 1); |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | +* 时间复杂度:令点数为 $n$,边数为 $m$。整体复杂度为 $O(n + m)$ |
| 111 | +* 空间复杂度:$O(n)$ |
| 112 | + |
| 113 | +--- |
| 114 | + |
| 115 | +### 迭代(通用非递归) |
| 116 | + |
| 117 | +上述的「迭代(非递归)」解法,我们还是需要针对题目再做简单分析。 |
| 118 | + |
| 119 | +而利用通用的「递归」转「非递归」技巧,我们可以直接对「递归」解法进行改写,同时利用通用非递归过程中的 `loc` 可以帮助我们省掉用作区分是否首次出队的 `set` 结构。 |
| 120 | + |
| 121 | +需要特别说明的是:**由于现代编译器已经做了很多关于递归的优化,现在这种技巧已经无须掌握。** |
| 122 | + |
| 123 | +代码: |
| 124 | +```Java |
| 125 | +class Solution { |
| 126 | + public String tree2str(TreeNode root) { |
| 127 | + StringBuilder sb = new StringBuilder(); |
| 128 | + Deque<Object[]> d = new ArrayDeque<>(); |
| 129 | + d.addLast(new Object[]{0, root}); |
| 130 | + while (!d.isEmpty()) { |
| 131 | + Object[] poll = d.pollLast(); |
| 132 | + int loc = (Integer)poll[0]; TreeNode t = (TreeNode)poll[1]; |
| 133 | + if (loc == 0) { |
| 134 | + sb.append("("); |
| 135 | + sb.append(t.val); |
| 136 | + d.addLast(new Object[]{1, t}); |
| 137 | + } else if (loc == 1) { |
| 138 | + d.addLast(new Object[]{2, t}); |
| 139 | + if (t.right != null) d.addLast(new Object[]{0, t.right}); |
| 140 | + if (t.left != null) d.addLast(new Object[]{0, t.left}); |
| 141 | + else if (t.right != null) sb.append("()"); |
| 142 | + } else if (loc == 2) { |
| 143 | + sb.append(")"); |
| 144 | + } |
| 145 | + } |
| 146 | + return sb.substring(1, sb.length() - 1); |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | +* 时间复杂度:令点数为 $n$,边数为 $m$。整体复杂度为 $O(n + m)$ |
| 151 | +* 空间复杂度:$O(n)$ |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +### 最后 |
| 156 | + |
| 157 | +这是我们「刷穿 LeetCode」系列文章的第 `No.606` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 |
| 158 | + |
| 159 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 160 | + |
| 161 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 162 | + |
| 163 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 164 | + |
0 commit comments