103. Binary Tree Zigzag Level Order Traversal 103. Binary Tree Zigzag Level Order Traversal - Medium
Description 103. Binary Tree Zigzag Level Order Traversal
Given the root
of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
Example 1 :
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[20,9],[15,7]]
还是一样的思路,层序遍历,加一个变量来控制方向就行。
Solution 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List < List < Integer >> zigzagLevelOrder ( TreeNode root ) {
if ( root == null ) return new ArrayList <> ();
Deque < TreeNode > q = new LinkedList <> ();
List < List < Integer >> ans = new ArrayList <> ();
q . offer ( root );
boolean f = true ;
while ( ! q . isEmpty ()) {
int size = q . size ();
LinkedList < Integer > list = new LinkedList ();
for ( int i = 0 ; i < size ; i ++ ) {
TreeNode node = q . poll ();
if ( f ) {
list . addLast ( node . val );
} else {
list . addFirst ( node . val );
}
if ( node . left != null ) q . offer ( node . left );
if ( node . right != null ) q . offer ( node . right );
}
f = ! f ;
ans . add ( list );
}
return ans ;
}
}