Skip to content

Commit e9f8861

Browse files
authored
Merge pull request gzc426#330 from Scottnan/master
Create Despacito.md
2 parents e1ebc13 + 381b3fe commit e9f8861

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

2018.12.07-leetcode104/Despacito.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# LeetCode 104 Maximum Depth of Binary Tree
2+
## 1. Description
3+
4+
Given a binary tree, find its maximum depth.
5+
6+
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
7+
8+
Note: A leaf is a node with no children.
9+
10+
Example:
11+
12+
Given binary tree [3,9,20,null,null,15,7],
13+
14+
3
15+
/ \
16+
9 20
17+
/ \
18+
15 7
19+
20+
return its depth = 3.
21+
22+
## 2. Solution
23+
24+
```python
25+
# Definition for a binary tree node.
26+
# class TreeNode:
27+
# def __init__(self, x):
28+
# self.val = x
29+
# self.left = None
30+
# self.right = None
31+
32+
class Solution:
33+
def maxDepth(self, root):
34+
"""
35+
:type root: TreeNode
36+
:rtype: int
37+
"""
38+
if root == None:
39+
return 0
40+
else:
41+
left = self.maxDepth(root.left)
42+
right = self.maxDepth(root.right)
43+
return max(left, right) + 1
44+
```

2018.12.08-leetcode105/Despacito.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal
2+
## 1. Description
3+
Given preorder and inorder traversal of a tree, construct the binary tree.
4+
5+
Note:
6+
You may assume that duplicates do not exist in the tree.
7+
8+
For example, given
9+
10+
preorder = [3,9,20,15,7]
11+
12+
inorder = [9,3,15,20,7]
13+
14+
Return the following binary tree:
15+
16+
3
17+
/ \
18+
9 20
19+
/ \
20+
15 7
21+
22+
## 2. Solution
23+
```python
24+
# Definition for a binary tree node.
25+
# class TreeNode:
26+
# def __init__(self, x):
27+
# self.val = x
28+
# self.left = None
29+
# self.right = None
30+
31+
class Solution:
32+
def buildTree(self, preorder, inorder):
33+
"""
34+
:type preorder: List[int]
35+
:type inorder: List[int]
36+
:rtype: TreeNode
37+
"""
38+
if len(preorder) == 0:
39+
return None
40+
if len(preorder) == 1:
41+
return TreeNode(preorder.pop(0))
42+
root = TreeNode(preorder.pop(0))
43+
root.left = self.buildTree(preorder[:inorder.index(root.val)], inorder[:inorder.index(root.val)])
44+
root.right = self.buildTree(preorder[inorder.index(root.val):], inorder[inorder.index(root.val)+1:])
45+
return root
46+
```

2018.12.09-leetcode106/Despacito.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
2+
## 1. Description
3+
4+
Given inorder and postorder traversal of a tree, construct the binary tree.
5+
6+
**Note:**
7+
8+
You may assume that duplicates do not exist in the tree.
9+
10+
For example, given
11+
12+
inorder = [9,3,15,20,7]
13+
14+
postorder = [9,15,7,20,3]
15+
16+
Return the following binary tree:
17+
18+
3
19+
/ \
20+
9 20
21+
/ \
22+
15 7
23+
24+
## 2. Solution
25+
```python
26+
# Definition for a binary tree node.
27+
# class TreeNode:
28+
# def __init__(self, x):
29+
# self.val = x
30+
# self.left = None
31+
# self.right = None
32+
33+
class Solution:
34+
def buildTree(self, inorder, postorder):
35+
"""
36+
:type inorder: List[int]
37+
:type postorder: List[int]
38+
:rtype: TreeNode
39+
"""
40+
if len(postorder) == 0:
41+
return None
42+
if len(postorder) == 1:
43+
return TreeNode(postorder[0])
44+
root = TreeNode(postorder.pop())
45+
root.left = self.buildTree(inorder[:inorder.index(root.val)], postorder[:inorder.index(root.val)])
46+
root.right = self.buildTree(inorder[inorder.index(root.val)+1:], postorder[inorder.index(root.val):])
47+
return root
48+
```

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)