File tree 5 files changed +134
-0
lines changed
algorithms/BinaryTreeZigzagLevelOrderTraversal
5 files changed +134
-0
lines changed Original file line number Diff line number Diff line change @@ -296,6 +296,7 @@ All solutions will be accepted!
296
296
| 75| [ Sort Colors] ( https://leetcode-cn.com/problems/sort-colors/description/ ) | [ java/py/js] ( ./algorithms/SortColors ) | Medium|
297
297
| 623| [ Add One Row To Tree] ( https://leetcode-cn.com/problems/add-one-row-to-tree/description/ ) | [ java/py/js] ( ./algorithms/AddOneRowToTree ) | Medium|
298
298
| 513| [ Find Bottom Left Tree Value] ( https://leetcode-cn.com/problems/find-bottom-left-tree-value/description/ ) | [ java/py/js] ( ./algorithms/FindBottomLeftTreeValue ) | Medium|
299
+ | 103| [ Binary Tree Zigzag Level Order Traversal] ( https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/description/ ) | [ java/py/js] ( ./algorithms/BinaryTreeZigzagLevelOrderTraversal ) | Medium|
299
300
300
301
# Database
301
302
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Binary Tree Zigzag Level Order Traversal
2
+ This problem is easy to solve by BFS
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+ class Solution {
11
+ public List <List <Integer >> zigzagLevelOrder (TreeNode root ) {
12
+ if (root == null )
13
+ return new ArrayList <List <Integer >>();
14
+
15
+ List <List <Integer >> res = new ArrayList <List <Integer >>();
16
+ List <Integer > values = new ArrayList <Integer >();
17
+ LinkedList <TreeNode > stack = new LinkedList <TreeNode >(),
18
+ nextStack = new LinkedList <TreeNode >();
19
+ int direction = 1 ;
20
+ stack .add (root );
21
+
22
+ while (stack .size () > 0 ) {
23
+ TreeNode node = stack .poll ();
24
+ if (direction == 1 )
25
+ values .add (node .val );
26
+ else
27
+ values .add (0 , node .val );
28
+
29
+ if (node .left != null )
30
+ nextStack .add (node .left );
31
+ if (node .right != null )
32
+ nextStack .add (node .right );
33
+
34
+ if (stack .size () == 0 ) {
35
+ direction = direction == 1 ? 0 : 1 ;
36
+ res .add (values );
37
+ values = new ArrayList <Integer >();
38
+ stack = nextStack ;
39
+ nextStack = new LinkedList <TreeNode >();
40
+ }
41
+ }
42
+
43
+ return res ;
44
+ }
45
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val) {
4
+ * this.val = val;
5
+ * this.left = this.right = null;
6
+ * }
7
+ */
8
+ /**
9
+ * @param {TreeNode } root
10
+ * @return {number[][] }
11
+ */
12
+ var zigzagLevelOrder = function ( root ) {
13
+ let stack = [ ] ,
14
+ nextStack = [ ] ,
15
+ direction = 1 ,
16
+ values = [ ] ,
17
+ res = [ ]
18
+
19
+ if ( root )
20
+ stack . push ( root )
21
+
22
+ while ( stack . length > 0 ) {
23
+ let node = stack . shift ( )
24
+ if ( direction )
25
+ values . push ( node . val )
26
+ else
27
+ values . unshift ( node . val )
28
+
29
+ if ( node . left )
30
+ nextStack . push ( node . left )
31
+ if ( node . right )
32
+ nextStack . push ( node . right )
33
+
34
+ if ( stack . length == 0 ) {
35
+ direction = direction ? 0 : 1
36
+ res . push ( values )
37
+ values = [ ]
38
+ stack = nextStack
39
+ nextStack = [ ]
40
+ }
41
+ }
42
+
43
+ return res
44
+ } ;
Original file line number Diff line number Diff line change
1
+ # Definition for a binary tree node.
2
+ # class TreeNode(object):
3
+ # def __init__(self, x):
4
+ # self.val = x
5
+ # self.left = None
6
+ # self.right = None
7
+
8
+ class Solution (object ):
9
+ def zigzagLevelOrder (self , root ):
10
+ """
11
+ :type root: TreeNode
12
+ :rtype: List[List[int]]
13
+ """
14
+ stack = []
15
+ if root :
16
+ stack .append (root )
17
+ next_stack = []
18
+ direction = 1
19
+ values = []
20
+ res = []
21
+
22
+ while len (stack ) > 0 :
23
+ node = stack [0 ]
24
+ stack .remove (node )
25
+ if direction :
26
+ values .append (node .val )
27
+ else :
28
+ values .insert (0 , node .val )
29
+
30
+ if node .left :
31
+ next_stack .append (node .left )
32
+ if node .right :
33
+ next_stack .append (node .right )
34
+
35
+ if len (stack ) == 0 :
36
+ direction = 0 if direction else 1
37
+ res .append (values )
38
+ values = []
39
+ stack = next_stack
40
+ next_stack = []
41
+
42
+ return res
You can’t perform that action at this time.
0 commit comments