|
| 1 | +### 题目描述 |
| 2 | + |
| 3 | +这是 LeetCode 上的 **[剑指 Offer 32 - I. 从上到下打印二叉树](https://leetcode.cn/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/solution/by-ac_oier-a9n5/)** ,难度为 **中等**。 |
| 4 | + |
| 5 | +Tag : 「二叉树」、「DFS」、「BFS」、「递归」、「迭代」 |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。 |
| 10 | + |
| 11 | +例如: |
| 12 | + |
| 13 | +给定二叉树: `[3,9,20,null,null,15,7]`, |
| 14 | +``` |
| 15 | + 3 |
| 16 | + / \ |
| 17 | + 9 20 |
| 18 | + / \ |
| 19 | + 15 7 |
| 20 | +``` |
| 21 | +返回: |
| 22 | +``` |
| 23 | +[3,9,20,15,7] |
| 24 | +``` |
| 25 | + |
| 26 | +提示: |
| 27 | +* `节点总数 <= 1000` |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +### 迭代 - BFS |
| 32 | + |
| 33 | +使用「迭代」进行求解是容易的,只需使用常规的 `BFS` 方法进行层序遍历即可。 |
| 34 | + |
| 35 | +Java 代码: |
| 36 | +```Java |
| 37 | +class Solution { |
| 38 | + public int[] levelOrder(TreeNode root) { |
| 39 | + List<Integer> list = new ArrayList<>(); |
| 40 | + Deque<TreeNode> d = new ArrayDeque<>(); |
| 41 | + if (root != null) d.addLast(root); |
| 42 | + while (!d.isEmpty()) { |
| 43 | + TreeNode t = d.pollFirst(); |
| 44 | + list.add(t.val); |
| 45 | + if (t.left != null) d.addLast(t.left); |
| 46 | + if (t.right != null) d.addLast(t.right); |
| 47 | + } |
| 48 | + int n = list.size(); |
| 49 | + int[] ans = new int[n]; |
| 50 | + for (int i = 0; i < n; i++) ans[i] = list.get(i); |
| 51 | + return ans; |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | +TypeScript 代码: |
| 56 | +```TypeScript |
| 57 | +function levelOrder(root: TreeNode | null): number[] { |
| 58 | + let he = 0, ta = 0 |
| 59 | + const ans: number[] = new Array<number>() |
| 60 | + const d: TreeNode[] = new Array<TreeNode>() |
| 61 | + if (root != null) d[ta++] = root |
| 62 | + while (he < ta) { |
| 63 | + const t = d[he++] |
| 64 | + ans.push(t.val) |
| 65 | + if (t.left != null) d[ta++] = t.left |
| 66 | + if (t.right != null) d[ta++] = t.right |
| 67 | + } |
| 68 | + return ans |
| 69 | +}; |
| 70 | +``` |
| 71 | +* 时间复杂度:$O(n)$ |
| 72 | +* 空间复杂度:$O(n)$ |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +### 递归 - DFS |
| 77 | + |
| 78 | +使用「递归」来进行「层序遍历」虽然不太符合直观印象,但也是可以的。 |
| 79 | + |
| 80 | +此时我们需要借助「哈希表」来存储起来每一层的节点情况。 |
| 81 | + |
| 82 | +首先我们按照「中序遍历」的方式进行 `DFS`,同时在 `DFS` 过程中传递节点所在的深度(`root` 节点默认在深度最小的第 $0$ 层),每次处理当前节点时,通过哈希表获取所在层的数组,并将当前节点值追加到数组尾部,同时维护一个最大深度 `max`,在 `DFS` 完成后,再使用深度范围 $[0, max]$ 从哈希表中进行构造答案。 |
| 83 | + |
| 84 | +Java 代码: |
| 85 | +```Java |
| 86 | +class Solution { |
| 87 | + Map<Integer, List<Integer>> map = new HashMap<>(); |
| 88 | + int max = -1, cnt = 0; |
| 89 | + public int[] levelOrder(TreeNode root) { |
| 90 | + dfs(root, 0); |
| 91 | + int[] ans = new int[cnt]; |
| 92 | + for (int i = 0, idx = 0; i <= max; i++) { |
| 93 | + for (int x : map.get(i)) ans[idx++] = x; |
| 94 | + } |
| 95 | + return ans; |
| 96 | + } |
| 97 | + void dfs(TreeNode root, int depth) { |
| 98 | + if (root == null) return ; |
| 99 | + max = Math.max(max, depth); |
| 100 | + cnt++; |
| 101 | + dfs(root.left, depth + 1); |
| 102 | + List<Integer> list = map.getOrDefault(depth, new ArrayList<Integer>()); |
| 103 | + list.add(root.val); |
| 104 | + map.put(depth, list); |
| 105 | + dfs(root.right, depth + 1); |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | +TypeScript 代码: |
| 110 | +```TypeScript |
| 111 | +const map:Map<number, Array<number>> = new Map<number, Array<number>>() |
| 112 | +let max = -1, cnt = 0 |
| 113 | +function levelOrder(root: TreeNode | null): number[] { |
| 114 | + map.clear() |
| 115 | + max = -1; cnt = 0; |
| 116 | + dfs(root, 0) |
| 117 | + const ans: Array<number> = new Array<number>() |
| 118 | + for (let i = 0; i <= max; i++) { |
| 119 | + for (const x of map.get(i)) ans.push(x) |
| 120 | + } |
| 121 | + return ans |
| 122 | +}; |
| 123 | +function dfs(root: TreeNode | null, depth: number): void { |
| 124 | + if (root == null) return |
| 125 | + max = Math.max(max, depth) |
| 126 | + cnt++ |
| 127 | + dfs(root.left, depth + 1) |
| 128 | + if (!map.has(depth)) map.set(depth, new Array<number>()) |
| 129 | + map.get(depth).push(root.val) |
| 130 | + dfs(root.right, depth + 1) |
| 131 | +} |
| 132 | +``` |
| 133 | +* 时间复杂度:$O(n)$ |
| 134 | +* 空间复杂度:$O(n)$ |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +### 最后 |
| 139 | + |
| 140 | +这是我们「刷穿 LeetCode」系列文章的第 `No.剑指 Offer 32 - I` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 |
| 141 | + |
| 142 | +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 |
| 143 | + |
| 144 | +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 |
| 145 | + |
| 146 | +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。 |
| 147 | + |
0 commit comments