File tree 5 files changed +112
-0
lines changed
algorithms/BinrayTreeLevelOrderTraversal
5 files changed +112
-0
lines changed Original file line number Diff line number Diff line change @@ -196,6 +196,7 @@ All solutions will be accepted!
196
196
| 665| [ Non Decreasing Array] ( https://leetcode-cn.com/problems/non-decreasing-array/description/ ) | [ java/py/js] ( ./algorithms/NonDecreasingArray ) | Easy|
197
197
| 867| [ Transpose Matrix] ( https://leetcode-cn.com/problems/transpose-matrix/description/ ) | [ java/py/js] ( ./algorithms/TransposeMatrix ) | Easy|
198
198
| 479| [ Largest Palindrome Product] ( https://leetcode-cn.com/problems/largest-palindrome-product/description/ ) | [ java/py/js] ( ./algorithms/LargestPalindromeProduct ) | Easy|
199
+ | 102| [ Binray Tree Level Order Traversal] ( https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/ ) | [ java/py/js] ( ./algorithms/BinrayTreeLevelOrderTraversal ) | Medium|
199
200
200
201
# Database
201
202
| #| Title| Solution| Difficulty|
Original file line number Diff line number Diff line change
1
+ # Binray Tree 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 >> levelOrder (TreeNode root ) {
12
+ List <List <Integer >> res = new ArrayList <List <Integer >>();
13
+ List <TreeNode > stack = new ArrayList <TreeNode >(),
14
+ nextStack = new ArrayList <TreeNode >();
15
+ List <Integer > values = new ArrayList <Integer >();
16
+
17
+ if (root != null ) stack .add (root );
18
+
19
+ while (stack .size () > 0 ) {
20
+ TreeNode node = stack .get (0 );
21
+ stack .remove (0 );
22
+ values .add (node .val );
23
+
24
+ if (node .left != null ) nextStack .add (node .left );
25
+ if (node .right != null ) nextStack .add (node .right );
26
+
27
+ if (stack .size () == 0 ) {
28
+ res .add (values );
29
+ stack = nextStack ;
30
+ values = new ArrayList <Integer >();
31
+ nextStack = new ArrayList <TreeNode >();
32
+ }
33
+ }
34
+
35
+ return res ;
36
+ }
37
+ }
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 levelOrder = function ( root ) {
13
+ let res = [ ] ,
14
+ stack = [ ] ,
15
+ nextStack = [ ] ,
16
+ values = [ ]
17
+
18
+ if ( root ) stack . push ( root )
19
+
20
+ while ( stack . length > 0 ) {
21
+ let node = stack . shift ( )
22
+ values . push ( node . val )
23
+
24
+ if ( node . left ) nextStack . push ( node . left )
25
+ if ( node . right ) nextStack . push ( node . right )
26
+
27
+ if ( stack . length === 0 ) {
28
+ res . push ( values )
29
+ stack = nextStack
30
+ values = [ ]
31
+ nextStack = [ ]
32
+ }
33
+ }
34
+
35
+ return res
36
+ } ;
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 levelOrder (self , root ):
10
+ """
11
+ :type root: TreeNode
12
+ :rtype: List[List[int]]
13
+ """
14
+ res = []
15
+ stack = []
16
+ next_stack = []
17
+ level_values = []
18
+ if root : stack .append (root )
19
+
20
+ while len (stack ) > 0 :
21
+ node = stack [0 ]
22
+ stack = stack [1 :]
23
+ level_values .append (node .val )
24
+
25
+ if node .left :
26
+ next_stack .append (node .left )
27
+ if node .right :
28
+ next_stack .append (node .right )
29
+
30
+ if len (stack ) == 0 :
31
+ res .append (level_values )
32
+ stack = next_stack
33
+ level_values = []
34
+ next_stack = []
35
+
36
+ return res
You can’t perform that action at this time.
0 commit comments