forked from KnowledgeCenterYoutube/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
107_BinaryTree_Level_Order_traversal_ii
67 lines (61 loc) · 1.98 KB
/
107_BinaryTree_Level_Order_traversal_ii
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
Letcode 107: Binary Tree Level Order traversal II
Detailed video explanation can be found here: https://youtu.be/FSWqYp3k1O4
C++
---
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
if(!root) return result;
queue<TreeNode*> Q;
Q.push(root);
while(!Q.empty()){
int n = Q.size();
vector<int> nodes(n);
for(int i = 0; i < n; ++i){
TreeNode* node = Q.front();
nodes[i] = node->val;
Q.pop();
if(node->left) Q.push(node->left);
if(node->right) Q.push(node->right);
}
result.push_back(nodes);
}
reverse(result.begin(), result.end());
return result;
}
Java:
----
public List<List<Integer>> levelOrderBottom(TreeNode root) {
ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
if(root == null) return result;
Queue<TreeNode> Q = new LinkedList<>();
Q.add(root);
while(!Q.isEmpty()){
int n = Q.size();
List<Integer> nodes = new ArrayList();
for(int i = 0; i < n; ++i){
TreeNode node = Q.poll();
nodes.add(node.val);
if(node.left != null) Q.add(node.left);
if(node.right != null) Q.add(node.right);
}
result.add(0, nodes);
}
return result;
}
Python3:
--------
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
result = []
if root == None: return result
Q = []
Q.append(root)
while len(Q) > 0:
n = len(Q)
nodes = []
for i in range(n):
node = Q.pop(0)
nodes.append(node.val)
if node.left != None: Q.append(node.left)
if node.right != None: Q.append(node.right)
result.insert(0, nodes)
return result