Skip to content

Commit db35ff3

Browse files
authoredOct 23, 2018
Added solutions
1 parent d7d8d44 commit db35ff3

File tree

7 files changed

+132
-0
lines changed

7 files changed

+132
-0
lines changed
 

‎100.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Runtime: 36 ms, faster than 91.77% of Python3 online submissions for Same Tree.
2+
# Difficulty: Easy
3+
4+
class Solution:
5+
def isSameTree(self, p, q):
6+
"""
7+
:type p: TreeNode
8+
:type q: TreeNode
9+
:rtype: bool
10+
"""
11+
if p == None and q == None:
12+
return True
13+
elif p == None or q == None:
14+
return False
15+
elif p.val != q.val:
16+
return False
17+
return Solution.isSameTree(None, p.right, q.right) and Solution.isSameTree(None, p.left, q.left)

‎111.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Runtime: 56 ms, faster than 74.98% of Python3 online submissions for Minimum Depth of Binary Tree.
2+
# Difficulty: Easy
3+
4+
class Solution:
5+
def minDepth(self, root):
6+
"""
7+
:type root: TreeNode
8+
:rtype: int
9+
"""
10+
if root == None:
11+
return 0
12+
elif root.left == None and root.right != None:
13+
return 1 + Solution.minDepth(self, root.right)
14+
elif root.left != None and root.right == None:
15+
return 1 + Solution.minDepth(self, root.left)
16+
elif root.left == None and root.right == None:
17+
return 1
18+
elif root.left != None and root.right != None:
19+
return 1 + min(Solution.minDepth(self, root.left), Solution.minDepth(self, root.right))

‎226.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Runtime: 36 ms, faster than 95.98% of Python3 online submissions for Invert Binary Tree.
2+
# Difficulty: Easy
3+
4+
class Solution:
5+
def invertTree(self, root):
6+
"""
7+
:type root: TreeNode
8+
:rtype: TreeNode
9+
"""
10+
if root == None:
11+
return None
12+
t = TreeNode(root.val)
13+
t.left = Solution.invertTree(None, root.right)
14+
t.right = Solution.invertTree(None, root.left)
15+
return t

‎257.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Runtime: 44 ms, faster than 59.47% of Python3 online submissions for Binary Tree Paths.
2+
# Difficulty: Easy
3+
4+
class Solution:
5+
def binaryTreePaths(self, root):
6+
"""
7+
:type root: TreeNode
8+
:rtype: List[str]
9+
"""
10+
paths = list()
11+
if root == None:
12+
return paths
13+
if root.right == None and root.left == None:
14+
return [str(root.val)]
15+
if root.left != None:
16+
for path in Solution.binaryTreePaths(None, root.left):
17+
paths.append(str(root.val) + '->' + path)
18+
if root.right != None:
19+
for path in Solution.binaryTreePaths(None, root.right):
20+
paths.append(str(root.val) + '->' + path)
21+
return paths

‎637.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Runtime: 92 ms, faster than 22.01% of Python3 online submissions for Average of Levels in Binary Tree.
2+
# Difficulty: Easy
3+
4+
class Solution:
5+
def averageOfLevels(self, root):
6+
"""
7+
:type root: TreeNode
8+
:rtype: List[float]
9+
"""
10+
values = list()
11+
def helper(tree, level):
12+
if tree == None:
13+
return
14+
if len(values) <= level:
15+
values.append([tree.val])
16+
else:
17+
values[level].append(tree.val)
18+
helper(tree.left, level + 1)
19+
helper(tree.right, level + 1)
20+
21+
helper(root, 0)
22+
averages = list()
23+
for value in values:
24+
averages.append(sum(value) / len(value))
25+
return averages

‎814.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Runtime: 40 ms, faster than 58.20% of Python3 online submissions for Binary Tree Pruning.
2+
# Difficulty: Medium
3+
4+
class Solution:
5+
def pruneTree(self, root):
6+
"""
7+
:type root: TreeNode
8+
:rtype: TreeNode
9+
"""
10+
def contains_one(tree):
11+
if tree == None:
12+
return False
13+
return any([contains_one(b) for b in [tree.right, tree.left]] + [tree.val == 1])
14+
15+
if contains_one(root):
16+
t = TreeNode(root.val)
17+
t.left = Solution.pruneTree(None, root.left)
18+
t.right = Solution.pruneTree(None, root.right)
19+
return t

‎94.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Runtime: 36 ms, faster than 96.42% of Python3 online submissions for Binary Tree Inorder Traversal.
2+
# Difficulty: Medium
3+
4+
class Solution:
5+
def inorderTraversal(self, root):
6+
"""
7+
:type root: TreeNode
8+
:rtype: List[int]
9+
"""
10+
lst = list()
11+
if root == None:
12+
return []
13+
lst.extend(Solution.inorderTraversal(None, root.left))
14+
lst.append(root.val)
15+
lst.extend(Solution.inorderTraversal(None, root.right))
16+
return lst

0 commit comments

Comments
 (0)
Please sign in to comment.