Skip to content

Commit c9a49fd

Browse files
committed
Merge branch 'master' of https://github.com/yuzhoujr/LeetCode
2 parents a1ae10b + 27ff8ab commit c9a49fd

11 files changed

+305
-0
lines changed

tree/113. Path Sum II - EricD.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 helper(self, node, num, path, res):
10+
left = num - node.val
11+
if left == 0 and (not node.left) and (not node.right):
12+
res.append(path+[node.val])
13+
elif (not node.left) and (not node.right):
14+
return
15+
else:
16+
if node.left: self.helper(node.left, left, path+[node.val],res)
17+
if node.right: self.helper(node.right, left, path+[node.val], res)
18+
19+
def pathSum(self, root, sum):
20+
"""
21+
:type root: TreeNode
22+
:type sum: int
23+
:rtype: List[List[int]]
24+
"""
25+
if not root:
26+
return []
27+
path,res = [],[]
28+
self.helper(root, sum, path, res)
29+
return res
30+
31+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def postorderTraversal(self, root):
2+
"""
3+
:type root: TreeNode
4+
:rtype: List[int]
5+
"""
6+
res,stack = [],[]
7+
while root:
8+
res.append(root.val)
9+
stack.append(root)
10+
root = root.right
11+
while stack:
12+
tmp = stack.pop()
13+
if tmp.left:
14+
t = tmp.left
15+
while t:
16+
res.append(t.val)
17+
stack.append(t)
18+
t = t.right
19+
return res[::-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 BSTIterator(object):
9+
def __init__(self, root):
10+
"""
11+
:type root: TreeNode
12+
"""
13+
self.list = []
14+
self.inOrder(root)
15+
self.i = 0
16+
17+
def inOrder(self,node):
18+
if not node:
19+
return
20+
self.inOrder(node.left)
21+
self.list.append(node.val)
22+
self.inOrder(node.right)
23+
24+
25+
def hasNext(self):
26+
"""
27+
:rtype: bool
28+
"""
29+
return self.i < len(self.list)
30+
31+
32+
def next(self):
33+
"""
34+
:rtype: int
35+
"""
36+
self.i+=1
37+
return self.list[self.i-1]
38+
39+
40+
# Your BSTIterator will be called like this:
41+
# i, v = BSTIterator(root), []
42+
# while i.hasNext(): v.append(i.next())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def helper(self,node,res):
2+
if not node:
3+
return 0
4+
l = self.helper(node.left,res)
5+
r = self.helper(node.right,res)
6+
res[0] = max(res[0],l+r)
7+
return 1+max(l,r)
8+
9+
def diameterOfBinaryTree(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
if not root:
15+
return 0
16+
res = [0]
17+
self.helper(root,res)
18+
return res[0]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def inorderTraversal(self, root):
2+
"""
3+
:type root: TreeNode
4+
:rtype: List[int]
5+
"""
6+
res = []
7+
stack = []
8+
while root:
9+
stack.append(root)
10+
root = root.left
11+
12+
while stack:
13+
tmp = stack.pop()
14+
res.append(tmp.val)
15+
if tmp.right:
16+
t = tmp.right
17+
#add all left
18+
while t:
19+
stack.append(t)
20+
t = t.left
21+
return res

tree/Ying/100_SameTree.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 isSameTree(self, p, q):
10+
"""
11+
:type p: TreeNode
12+
:type q: TreeNode
13+
:rtype: bool
14+
"""
15+
if not p and not q:
16+
return True
17+
18+
return self.isSameTreeHelper(p, q)
19+
20+
def isSameTreeHelper(self, p, q):
21+
22+
if not p and not q:
23+
return True
24+
25+
if (not p and q) or (not q and p):
26+
return False
27+
28+
29+
return p.val==q.val and self.isSameTreeHelper(p.left, q.left) and self.isSameTreeHelper(p.right, q.right)

tree/Ying/101_SymmetricTree.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 isSymmetric(self, root):
10+
11+
if not root:
12+
return True
13+
14+
return self.compareTree(root.left, root.right)
15+
16+
17+
18+
def compareTree(self, left, right):
19+
20+
if not left and not right:
21+
return True
22+
23+
24+
25+
26+
if (not left and right) or (not right and left):
27+
return False
28+
29+
30+
31+
32+
return left.val==right.val and self.compareTree(left.left, right.right) and self.compareTree(left.right, right.left)
33+
34+
35+
36+

tree/Ying/104_MaxDepth.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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 maxDepth(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
if root is None:
15+
return 0
16+
17+
elif root.left is None and root.right is None:
18+
return 1
19+
else:
20+
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1

tree/Ying/111_MinDepth.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 minDepth(self, root):
10+
"""a
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
if not root:
15+
return 0
16+
17+
return self.minDepthHelper(root)
18+
19+
def minDepthHelper(self, root):
20+
21+
22+
23+
if not root:
24+
return 0
25+
26+
27+
if not root.left and not root.right:
28+
return 1
29+
30+
left = self.minDepthHelper(root.left)
31+
right = self.minDepthHelper(root.right)
32+
33+
return max(left,right )+1 if left==0 or right==0 else min(left, right)+1
34+
35+
36+
37+
38+

tree/Ying/235_LCA.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 lowestCommonAncestor(self, root, p, q):
10+
"""
11+
:type root: TreeNode
12+
:type p: TreeNode
13+
:type q: TreeNode
14+
:rtype: TreeNode
15+
"""
16+
if not root or not p or not q:
17+
return root
18+
19+
maxval = max(p.val, q.val)
20+
minval = min(p.val, q.val)
21+
22+
while root:
23+
24+
if minval<=root.val<=maxval:
25+
return root
26+
27+
elif root.val>maxval:
28+
root = root.left
29+
30+
elif root.val<minval:
31+
root = root.right
32+
33+
34+
35+
return root
36+
37+

tree/Yu/104_maxDepth.py

+14
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,17 @@ def maxDepth(self, root):
2020
return 0
2121
else:
2222
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
23+
24+
25+
# ********************
26+
# Divide and Conquer *
27+
# ********************
28+
class Solution(object):
29+
def maxDepth(self, root):
30+
if not root:
31+
return 0
32+
else:
33+
# using Divide and Conquer
34+
left = self.maxDepth(root.left)
35+
right = self.maxDepth(root.right)
36+
return max(left, right) + 1

0 commit comments

Comments
 (0)