Skip to content

Commit a191dd3

Browse files
committed
2018.12.10 LeetCode 107
1 parent d1d7da5 commit a191dd3

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

2018.12.10-leetcode107/Despacito.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# LeetCode 107 Binary Tree Level Order Traversal II
2+
## 1. Description
3+
4+
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
5+
6+
For example:
7+
Given binary tree [3,9,20,null,null,15,7],
8+
9+
3
10+
/ \
11+
9 20
12+
/ \
13+
15 7
14+
15+
return its bottom-up level order traversal as:
16+
17+
```
18+
[
19+
[15,7],
20+
[9,20],
21+
[3]
22+
]
23+
```
24+
25+
## 2. Solution
26+
```python
27+
# Definition for a binary tree node.
28+
# class TreeNode:
29+
# def __init__(self, x):
30+
# self.val = x
31+
# self.left = None
32+
# self.right = None
33+
34+
class Solution:
35+
def levelOrderBottom(self, root):
36+
"""
37+
:type root: TreeNode
38+
:rtype: List[List[int]]
39+
"""
40+
if root == None:
41+
return []
42+
tmp = [root, '#']
43+
level = []
44+
result = []
45+
while len(tmp) > 1:
46+
head = tmp.pop(0)
47+
if head != '#':
48+
level.append(head.val)
49+
if head.left:
50+
tmp.append(head.left)
51+
if head.right:
52+
tmp.append(head.right)
53+
else:
54+
tmp.append('#')
55+
result.insert(0, level)
56+
level = []
57+
result.insert(0, level)
58+
return result
59+
```

0 commit comments

Comments
 (0)