|
| 1 | +# 题目描述(中等难度) |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +和 [102 题](<https://leetcode.wang/leetcode-102-Binary-Tree-Level-Order-Traversal.html>) 类似,二叉树的层次遍历。只不过这题要求,第 1 层从左到右,第 2 层从右到左,第 3 层从左到右,第 4 层从右到左,交替进行。 |
| 6 | + |
| 7 | +# 思路分析 |
| 8 | + |
| 9 | +大家可以先做下 [102 题](<https://leetcode.wang/leetcode-102-Binary-Tree-Level-Order-Traversal.html>) 吧,直接在 102 题的基础上进行修改即可。从左到右和从右到左交替,所以我们只需要判断当前的 `level`,层数从 0 开始的话,偶数就把元素添加到当前层的末尾,奇数的话,每次把新元素添加到头部,这样就实现了从右到左的遍历。 |
| 10 | + |
| 11 | +# 解法一 DFS |
| 12 | + |
| 13 | +判断 level 是偶数还是奇数即可。 |
| 14 | + |
| 15 | +```java |
| 16 | +public List<List<Integer>> zigzagLevelOrder(TreeNode root) { |
| 17 | + List<List<Integer>> ans = new ArrayList<>(); |
| 18 | + DFS(root, 0, ans); |
| 19 | + return ans; |
| 20 | +} |
| 21 | + |
| 22 | +private void DFS(TreeNode root, int level, List<List<Integer>> ans) { |
| 23 | + if (root == null) { |
| 24 | + return; |
| 25 | + } |
| 26 | + if (ans.size() <= level) { |
| 27 | + ans.add(new ArrayList<>()); |
| 28 | + } |
| 29 | + if ((level % 2) == 0) { |
| 30 | + ans.get(level).add(root.val); //添加到末尾 |
| 31 | + } else { |
| 32 | + ans.get(level).add(0, root.val); //添加到头部 |
| 33 | + } |
| 34 | + |
| 35 | + DFS(root.left, level + 1, ans); |
| 36 | + DFS(root.right, level + 1, ans); |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +# 解法二 BFS 队列 |
| 41 | + |
| 42 | +如果是顺序刷题,前边的 [97 题](https://leetcode.wang/leetCode-97-Interleaving-String.html#%E8%A7%A3%E6%B3%95%E4%B8%89-%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E9%81%8D%E5%8E%86-bfs),[ 98 题](https://leetcode.wang/leetCode-98-Validate-Binary-Search-Tree.html#%E8%A7%A3%E6%B3%95%E4%B8%89-dfs-bfs),[101 题](https://leetcode.wang/leetcode-101-Symmetric-Tree.html#%E8%A7%A3%E6%B3%95%E4%B8%89-bfs-%E9%98%9F%E5%88%97),都用到了 BFS ,应该很熟悉了。 |
| 43 | + |
| 44 | +之前我们用一个 `while` 循环,不停的从队列中拿一个节点,并且在循环中将当前取出来的节点的左孩子和右孩子也加入到队列中。 |
| 45 | + |
| 46 | +相比于这道题,我们要解决的问题是,怎么知道当前节点的 `level` 。 |
| 47 | + |
| 48 | +## 第一种方案 |
| 49 | + |
| 50 | +定义一个新的 class,class 里边两个成员 node 和 level,将我们新定义的 class 每次加入到队列中。或者用一个新的队列和之前的节点队列同步入队出队,新的队列存储 level。 |
| 51 | + |
| 52 | +下边的代码实现后一种,并且对 level 进行判断。 |
| 53 | + |
| 54 | +```java |
| 55 | +public List<List<Integer>> zigzagLevelOrder(TreeNode root) { |
| 56 | + List<List<Integer>> ans = new ArrayList<>(); |
| 57 | + if (root == null) { |
| 58 | + return ans; |
| 59 | + } |
| 60 | + Queue<TreeNode> treeNode = new LinkedList<>(); |
| 61 | + Queue<Integer> nodeLevel = new LinkedList<>(); |
| 62 | + treeNode.offer(root); |
| 63 | + int level = 0; |
| 64 | + nodeLevel.offer(level); |
| 65 | + while (!treeNode.isEmpty()) { |
| 66 | + TreeNode curNode = treeNode.poll(); |
| 67 | + int curLevel = nodeLevel.poll(); |
| 68 | + if (curNode != null) { |
| 69 | + if (ans.size() <= curLevel) { |
| 70 | + ans.add(new ArrayList<>()); |
| 71 | + } |
| 72 | + if ((curLevel % 2) == 0) { |
| 73 | + ans.get(curLevel).add(curNode.val); |
| 74 | + } else { |
| 75 | + ans.get(curLevel).add(0, curNode.val); |
| 76 | + } |
| 77 | + level = curLevel + 1; |
| 78 | + treeNode.offer(curNode.left); |
| 79 | + nodeLevel.offer(level); |
| 80 | + treeNode.offer(curNode.right); |
| 81 | + nodeLevel.offer(level); |
| 82 | + } |
| 83 | + } |
| 84 | + return ans; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +# 第二种方案 |
| 89 | + |
| 90 | +把 [102 题](<https://leetcode.wang/leetcode-102-Binary-Tree-Level-Order-Traversal.html>) 的解释贴过来。 |
| 91 | + |
| 92 | +> 我们在 while 循环中加一个 for 循环,循环次数是循环前的队列中的元素个数即可,使得每次的 while 循环出队的元素都是同一层的元素。 |
| 93 | +> |
| 94 | +> for 循环结束也就意味着当前层结束了,而此时的队列存储的元素就是下一层的所有元素了。 |
| 95 | +
|
| 96 | +这道题我们要知道当前应该是从左到右还是从右到左,最直接的方案当然是增加一个 `level` 变量,和上边的解法一样,来判断 `level` 是奇数还是偶数即可。 |
| 97 | + |
| 98 | +```java |
| 99 | +public List<List<Integer>> zigzagLevelOrder(TreeNode root) { |
| 100 | + Queue<TreeNode> queue = new LinkedList<TreeNode>(); |
| 101 | + List<List<Integer>> ans = new LinkedList<List<Integer>>(); |
| 102 | + if (root == null) |
| 103 | + return ans; |
| 104 | + queue.offer(root); |
| 105 | + while (!queue.isEmpty()) { |
| 106 | + int levelNum = queue.size(); // 当前层元素的个数 |
| 107 | + List<Integer> subList = new LinkedList<Integer>(); |
| 108 | + int level = 0; |
| 109 | + for (int i = 0; i < levelNum; i++) { |
| 110 | + TreeNode curNode = queue.poll(); |
| 111 | + if (curNode != null) { |
| 112 | + if ((level % 2) == 0) { |
| 113 | + subList.add(curNode.val); |
| 114 | + } else { |
| 115 | + subList.add(0, curNode.val); |
| 116 | + } |
| 117 | + queue.offer(curNode.left); |
| 118 | + queue.offer(curNode.right); |
| 119 | + } |
| 120 | + } |
| 121 | + if (subList.size() > 0) { |
| 122 | + ans.add(subList); |
| 123 | + } |
| 124 | + level++; |
| 125 | + } |
| 126 | + return ans; |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +除了增加 `level` 变量外,我们还可以增加一个 `boolean` 变量来区别当前从左还是从右。此外 [这里](<https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/discuss/33815/My-accepted-JAVA-solution>) 的评论里,看到了另外一种想法,不用添加新的变量。我们直接判断当前 `ans` 的大小,如果大小是 n 代表当前在添加第 n 层。 |
| 131 | + |
| 132 | +# 解法三 |
| 133 | + |
| 134 | +[这里](<https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/discuss/33904/JAVA-Double-Stack-Solution>) 看到一个有趣的想法,分享一下。 |
| 135 | + |
| 136 | +我们直接用两个栈(或者队列)轮换着添加元素,一个栈从左到右添加元素,一个栈从右到左添加元素。 |
| 137 | + |
| 138 | +```java |
| 139 | +public List<List<Integer>> zigzagLevelOrder(TreeNode root) { |
| 140 | + TreeNode c=root; |
| 141 | + List<List<Integer>> ans =new ArrayList<List<Integer>>(); |
| 142 | + if(c==null) return ans; |
| 143 | + Stack<TreeNode> s1=new Stack<TreeNode>(); |
| 144 | + Stack<TreeNode> s2=new Stack<TreeNode>(); |
| 145 | + s1.push(root); |
| 146 | + while(!s1.isEmpty()||!s2.isEmpty()) |
| 147 | + { |
| 148 | + List<Integer> tmp=new ArrayList<Integer>(); |
| 149 | + while(!s1.isEmpty()) |
| 150 | + { |
| 151 | + c=s1.pop(); |
| 152 | + tmp.add(c.val); |
| 153 | + if(c.left!=null) s2.push(c.left); |
| 154 | + if(c.right!=null) s2.push(c.right); |
| 155 | + } |
| 156 | + ans.add(tmp); |
| 157 | + tmp=new ArrayList<Integer>(); |
| 158 | + while(!s2.isEmpty()) |
| 159 | + { |
| 160 | + c=s2.pop(); |
| 161 | + tmp.add(c.val); |
| 162 | + if(c.right!=null)s1.push(c.right); |
| 163 | + if(c.left!=null)s1.push(c.left); |
| 164 | + } |
| 165 | + if(!tmp.isEmpty()) ans.add(tmp); |
| 166 | + } |
| 167 | + return ans; |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +# 总 |
| 172 | + |
| 173 | +这道题和 [102 题](<https://leetcode.wang/leetcode-102-Binary-Tree-Level-Order-Traversal.html>) 区别不大,只需要对当前层进行判断即可。解法三用两个栈还是蛮有意思的。 |
0 commit comments