Skip to content

Commit d4d0f55

Browse files
committed
update at 2018-04-02
1 parent 7cd19a6 commit d4d0f55

File tree

40 files changed

+1626
-1
lines changed

40 files changed

+1626
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
5+
#
6+
#
7+
# For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
8+
#
9+
# 1
10+
# / \
11+
# 2 2
12+
# / \ / \
13+
# 3 4 4 3
14+
#
15+
#
16+
#
17+
# But the following [1,2,2,null,3,null,3] is not:
18+
#
19+
# 1
20+
# / \
21+
# 2 2
22+
# \ \
23+
# 3 3
24+
#
25+
#
26+
#
27+
#
28+
# Note:
29+
# Bonus points if you could solve it both recursively and iteratively.
30+
#
31+
32+
33+
# Definition for a binary tree node.
34+
# class TreeNode(object):
35+
# def __init__(self, x):
36+
# self.val = x
37+
# self.left = None
38+
# self.right = None
39+
40+
class Solution(object):
41+
def isSymmetric(self, root):
42+
"""
43+
:type root: TreeNode
44+
:rtype: bool
45+
"""
46+
return self.helper(root,root)
47+
48+
def helper(self,root1,root2):
49+
if not root1 and not root2:
50+
return True
51+
if not root1 or not root2:
52+
return False
53+
if root1.val != root2.val:
54+
return False
55+
return self.helper(root1.left,root2.right) and self.helper(root1.right,root2.left)
56+
return res
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding:utf-8 -*-
2+
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+
# For example:
9+
# Given binary tree [3,9,20,null,null,15,7],
10+
#
11+
#
12+
# 3
13+
# / \
14+
# 9 20
15+
# / \
16+
# 15 7
17+
#
18+
# return its depth = 3.
19+
#
20+
21+
22+
# Definition for a binary tree node.
23+
# class TreeNode(object):
24+
# def __init__(self, x):
25+
# self.val = x
26+
# self.left = None
27+
# self.right = None
28+
29+
class Solution(object):
30+
def maxDepth(self, root):
31+
"""
32+
:type root: TreeNode
33+
:rtype: int
34+
"""
35+
if not root:
36+
return 0
37+
# if root.left == None and root.right == None:
38+
# return 1
39+
else:
40+
return max(self.maxDepth(root.left), self.maxDepth(root.right))+1
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# -*- coding:utf-8 -*-
2+
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+
#
7+
# For example:
8+
# Given binary tree [3,9,20,null,null,15,7],
9+
#
10+
# 3
11+
# / \
12+
# 9 20
13+
# / \
14+
# 15 7
15+
#
16+
#
17+
#
18+
# return its bottom-up level order traversal as:
19+
#
20+
# [
21+
# [15,7],
22+
# [9,20],
23+
# [3]
24+
# ]
25+
#
26+
#
27+
28+
29+
# Definition for a binary tree node.
30+
# class TreeNode(object):
31+
# def __init__(self, x):
32+
# self.val = x
33+
# self.left = None
34+
# self.right = None
35+
36+
class Solution(object):
37+
rlst = []
38+
39+
def levelOrderBottom(self, root):
40+
"""
41+
:type root: TreeNode
42+
:rtype: List[List[int]]
43+
"""
44+
if not root:
45+
return []
46+
self.rlst=[]
47+
self.levelList(root, 0)
48+
mx = max([item['hight'] for item in self.rlst])
49+
rst = [list() for _ in range(mx+1)]
50+
for item in self.rlst:
51+
rst[mx - item['hight']].append(item['val'])
52+
53+
return rst
54+
55+
def levelList(self, root, hight):
56+
if root:
57+
self.rlst.append({'val': root.val, 'hight': hight})
58+
hight = hight + 1
59+
if root.left:
60+
self.levelList(root.left, hight)
61+
if root.right:
62+
self.levelList(root.right, hight)
63+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
5+
#
6+
# For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
7+
#
8+
#
9+
#
10+
#
11+
# Example:
12+
#
13+
# Given the sorted array: [-10,-3,0,5,9],
14+
#
15+
# One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
16+
#
17+
# 0
18+
# / \
19+
# -3 9
20+
# / /
21+
# -10 5
22+
#
23+
#
24+
25+
26+
# Definition for a binary tree node.
27+
# class TreeNode(object):
28+
# def __init__(self, x):
29+
# self.val = x
30+
# self.left = None
31+
# self.right = None
32+
33+
class Solution(object):
34+
def sortedArrayToBST(self, nums):
35+
"""
36+
:type nums: List[int]
37+
:rtype: TreeNode
38+
"""
39+
if not nums:
40+
return None
41+
42+
mid = len(nums) // 2
43+
44+
root = TreeNode(nums[mid])
45+
root.left = self.sortedArrayToBST(nums[:mid])
46+
root.right = self.sortedArrayToBST(nums[mid+1:])
47+
48+
return root
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Given a binary tree, find its minimum depth.
5+
#
6+
# The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
7+
8+
9+
# Definition for a binary tree node.
10+
# class TreeNode(object):
11+
# def __init__(self, x):
12+
# self.val = x
13+
# self.left = None
14+
# self.right = None
15+
16+
class Solution(object):
17+
def minDepth(self, root):
18+
"""
19+
:type root: TreeNode
20+
:rtype: int
21+
"""
22+
if root == None:
23+
return 0
24+
if root.left==None or root.right==None:
25+
return self.minDepth(root.left)+self.minDepth(root.right)+1
26+
return min(self.minDepth(root.right),self.minDepth(root.left))+1

112-path-sum/path-sum.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
5+
#
6+
# For example:
7+
# Given the below binary tree and sum = 22,
8+
#
9+
#
10+
# 5
11+
# / \
12+
# 4 8
13+
# / / \
14+
# 11 13 4
15+
# / \ \
16+
# 7 2 1
17+
#
18+
#
19+
# return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
20+
#
21+
22+
23+
# Definition for a binary tree node.
24+
# class TreeNode(object):
25+
# def __init__(self, x):
26+
# self.val = x
27+
# self.left = None
28+
# self.right = None
29+
30+
class Solution(object):
31+
def hasPathSum(self, root, sum):
32+
"""
33+
:type root: TreeNode
34+
:type sum: int
35+
:rtype: bool
36+
"""
37+
if not root:
38+
return False
39+
40+
stack = [(root, sum)]
41+
while stack:
42+
node, sum = stack.pop()
43+
if not node.left and not node.right and node.val == sum:
44+
return True
45+
46+
if node.left:
47+
stack.append((node.left, sum-node.val))
48+
if node.right:
49+
stack.append((node.right, sum-node.val))
50+
51+
return False

113-path-sum-ii/path-sum-ii.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
#
5+
# Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
6+
#
7+
#
8+
# For example:
9+
# Given the below binary tree and sum = 22,
10+
#
11+
# 5
12+
# / \
13+
# 4 8
14+
# / / \
15+
# 11 13 4
16+
# / \ / \
17+
# 7 2 5 1
18+
#
19+
#
20+
#
21+
# return
22+
#
23+
# [
24+
# [5,4,11,2],
25+
# [5,8,4,5]
26+
# ]
27+
#
28+
#
29+
30+
31+
# Definition for a binary tree node.
32+
# class TreeNode(object):
33+
# def __init__(self, x):
34+
# self.val = x
35+
# self.left = None
36+
# self.right = None
37+
38+
class Solution(object):
39+
def pathSum(self, root, sum):
40+
"""
41+
:type root: TreeNode
42+
:type sum: int
43+
:rtype: List[List[int]]
44+
"""
45+
if not root:
46+
return []
47+
48+
if not root.left and not root.right and root.val == sum:
49+
return [[root.val]]
50+
51+
r_left = []
52+
r_right = []
53+
54+
if root.left:
55+
r_left = [[root.val] + l for l in self.pathSum(root.left, sum-root.val)]
56+
57+
if root.right:
58+
r_right = [[root.val] + l for l in self.pathSum(root.right, sum-root.val)]
59+
60+
return r_left + r_right
61+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding:utf-8 -*-
2+
3+
4+
# Given numRows, generate the first numRows of Pascal's triangle.
5+
#
6+
#
7+
# For example, given numRows = 5,
8+
# Return
9+
#
10+
# [
11+
# [1],
12+
# [1,1],
13+
# [1,2,1],
14+
# [1,3,3,1],
15+
# [1,4,6,4,1]
16+
# ]
17+
#
18+
#
19+
20+
21+
class Solution(object):
22+
def generate(self, numRows):
23+
"""
24+
:type numRows: int
25+
:rtype: List[List[int]]
26+
"""
27+
if numRows == 0:
28+
return []
29+
30+
if numRows == 1:
31+
return [[1]]
32+
33+
tmp = self.generate(numRows-1)
34+
# x = [0] + tmp[-1]
35+
# y = tmp[-1] + [0]
36+
# a = [x[i]+y[i] for i,_ in enumerate(x)]
37+
a = list(map(lambda x, y: x+y, tmp[-1] + [0], [0] + tmp[-1]))
38+
return tmp + [a]

0 commit comments

Comments
 (0)