Skip to content

Commit

Permalink
Merge 4ea6b46 into d31ec0a
Browse files Browse the repository at this point in the history
  • Loading branch information
Ernest committed Feb 13, 2019
2 parents d31ec0a + 4ea6b46 commit 752d3b9
Show file tree
Hide file tree
Showing 21 changed files with 143 additions and 158 deletions.
1 change: 1 addition & 0 deletions algorithms/matrix/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

20 changes: 4 additions & 16 deletions algorithms/tree/bin_tree_to_list.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
class Node():
def __init__(self, val = 0):
self.val = val
self.left = None
self.right = None
from tree.tree import TreeNode


def bin_tree_to_list(root):
"""
Expand All @@ -15,6 +12,7 @@ def bin_tree_to_list(root):
root = root.left
return root


def bin_tree_to_list_util(root):
if not root:
return root
Expand All @@ -32,18 +30,8 @@ def bin_tree_to_list_util(root):
root.right = right
return root


def print_tree(root):
while root:
print(root.val)
root = root.right

tree = Node(10)
tree.left = Node(12)
tree.right = Node(15)
tree.left.left = Node(25)
tree.left.left.right = Node(100)
tree.left.right = Node(30)
tree.right.left = Node(36)

head = bin_tree_to_list(tree)
print_tree(head)
5 changes: 3 additions & 2 deletions algorithms/tree/binary_tree_paths.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
def binary_tree_paths(root):
res = []
if not root:
if root is None:
return res
dfs(res, root, str(root.val))
return res


def dfs(res, root, cur):
if not root.left and not root.right:
if root.left is None and root.right is None:
res.append(cur)
if root.left:
dfs(res, root.left, cur+'->'+str(root.left.val))
Expand Down
33 changes: 16 additions & 17 deletions algorithms/tree/deepest_left.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@
# 7
# should return 4.

from tree.tree import TreeNode

class Node:
def __init__(self, val = None):
self.left = None
self.right = None
self.val = val

class DeepestLeft:
def __init__(self):
self.depth = 0
self.Node = None


def find_deepest_left(root, is_left, depth, res):
if not root:
return
Expand All @@ -33,15 +30,17 @@ def find_deepest_left(root, is_left, depth, res):
find_deepest_left(root.left, True, depth + 1, res)
find_deepest_left(root.right, False, depth + 1, res)

root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.right.right = Node(6)
root.right.right.right = Node(7)

res = DeepestLeft()
find_deepest_left(root, True, 1, res)
if res.Node:
print(res.Node.val)

if __name__ == '__main__':
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.right = TreeNode(6)
root.right.right.right = TreeNode(7)

res = DeepestLeft()
find_deepest_left(root, True, 1, res)
if res.Node:
print(res.Node.val)
2 changes: 1 addition & 1 deletion algorithms/tree/invert_tree.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# invert a binary tree

def reverse(root):
if not root:
if root is None:
return
root.left, root.right = root.right, root.left
if root.left:
Expand Down
41 changes: 22 additions & 19 deletions algorithms/tree/is_balanced.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@

def is_balanced(root):
return __is_balanced_recursive(root)


def __is_balanced_recursive(root):
"""
O(N) solution
"""
return -1 != get_depth(root)
return -1 != __get_depth(root)


def get_depth(root):
def __get_depth(root):
"""
return 0 if unbalanced else depth + 1
"""
if not root:
if root is None:
return 0
left = get_depth(root.left)
right = get_depth(root.right)
if abs(left-right) > 1 or left == -1 or right == -1:
left = __get_depth(root.left)
right = __get_depth(root.right)
if abs(left-right) > 1 or -1 in [left, right]:
return -1
return 1 + max(left, right)

################################

def is_balanced(root):
"""
O(N^2) solution
"""
left = max_height(root.left)
right = max_height(root.right)
return abs(left-right) <= 1 and is_balanced(root.left) and is_balanced(root.right)
# def is_balanced(root):
# """
# O(N^2) solution
# """
# left = max_height(root.left)
# right = max_height(root.right)
# return abs(left-right) <= 1 and is_balanced(root.left) and is_balanced(root.right)

def max_height(root):
if not root:
return 0
return max(max_height(root.left), max_height(root.right)) + 1
# def max_height(root):
# if root is None:
# return 0
# return max(max_height(root.left), max_height(root.right)) + 1
6 changes: 2 additions & 4 deletions algorithms/tree/is_subtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ def is_subtree(big, small):


def comp(p, q):
if not p and not q:
if p is None and q is None:
return True
if p and q:
if p is not None and q is not None:
return p.val == q.val and comp(p.left,q.left) and comp(p.right, q.right)
return False


12 changes: 6 additions & 6 deletions algorithms/tree/is_symmetric.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@

# TC: O(b) SC: O(log n)
def is_symmetric(root):
if not root:
if root is None:
return True
return helper(root.left, root.right)


def helper(p, q):
if not p and not q:
if p is None and q is None:
return True
if not p or not q or q.val != p.val:
if p is not None or q is not None or q.val != p.val:
return False
return helper(p.left, q.right) and helper(p.right, q.left)


def is_symmetric_iterative(root):
if not root:
if root is None:
return True
stack = [[root.left, root.right]]
while stack:
left, right = stack.pop() # popleft
if not left and not right:
if left is None and right is None:
continue
if not left or not right:
if left is None or right is None:
return False
if left.val == right.val:
stack.append([left.left, right.right])
Expand Down
4 changes: 2 additions & 2 deletions algorithms/tree/longest_consecutive.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ def longest_consecutive(root):
:type root: TreeNode
:rtype: int
"""
if not root:
if root is None:
return 0
max_len = 0
dfs(root, 0, root.val, max_len)
return max_len


def dfs(root, cur, target, max_len):
if not root:
if root is None:
return
if root.val == target:
cur += 1
Expand Down
4 changes: 2 additions & 2 deletions algorithms/tree/lowest_common_ancestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def lca(root, p, q):
:type q: TreeNode
:rtype: TreeNode
"""
if not root or root is p or root is q:
if root is None or root is p or root is q:
return root
left = lca(root.left, p, q)
right = lca(root.right, p, q)
if left and right:
if left is not None and right is not None:
return root
return left if left else right
50 changes: 25 additions & 25 deletions algorithms/tree/max_height.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@
longest path from the root node down to the farthest leaf node.
"""


class Node():
def __init__(self, val = 0):
self.val = val
self.left = None
self.right = None

# def max_height(root):
# if not root:
# return 0
# return max(maxDepth(root.left), maxDepth(root.right)) + 1
# if not root:
# return 0
# return max(maxDepth(root.left), maxDepth(root.right)) + 1

# iterative

from tree.tree import TreeNode


def max_height(root):
if not root:
if root is None:
return 0
height = 0
queue = [root]
Expand All @@ -28,27 +25,30 @@ def max_height(root):
level = []
while queue:
node = queue.pop(0)
if node.left:
if node.left is not None:
level.append(node.left)
if node.right:
if node.right is not None:
level.append(node.right)
queue = level
return height


def print_tree(root):
if root:
if root is not None:
print(root.val)
print_tree(root.left)
print_tree(root.right)

tree = Node(10)
tree.left = Node(12)
tree.right = Node(15)
tree.left.left = Node(25)
tree.left.left.right = Node(100)
tree.left.right = Node(30)
tree.right.left = Node(36)

height = max_height(tree)
print_tree(tree)
print("height:", height)

if __name__ == '__main__':
tree = TreeNode(10)
tree.left = TreeNode(12)
tree.right = TreeNode(15)
tree.left.left = TreeNode(25)
tree.left.left.right = TreeNode(100)
tree.left.right = TreeNode(30)
tree.right.left = TreeNode(36)

height = max_height(tree)
print_tree(tree)
print("height:", height)
4 changes: 1 addition & 3 deletions algorithms/tree/max_path_sum.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@


def max_path_sum(root):
maximum = float("-inf")
helper(root, maximum)
return maximum


def helper(root, maximum):
if not root:
if root is None:
return 0
left = helper(root.left, maximum)
right = helper(root.right, maximum)
Expand Down

0 comments on commit 752d3b9

Please sign in to comment.