Skip to content

Commit

Permalink
day 8 LCA
Browse files Browse the repository at this point in the history
  • Loading branch information
mandliya committed Jul 1, 2019
1 parent 11d6841 commit 81f08aa
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
| Current Status| Stats |
| :------------: | :----------: |
| Total C++ Problems | 188 |
| Total Python Problems | 11 |
| Total Python Problems | 12 |
| Current Daily Streak| 7 |
| Last Streak | 06/20/2019 - 06/21/2019|
| Current Streak | 06/23/2019 - 06/29/2019|
| Current Streak | 06/23/2019 - 06/30/2019|

</center>

Expand Down Expand Up @@ -119,7 +119,7 @@ Include contains single header implementation of data structures and some algori
|Recursive Level order traveral of Tree | [levelOrderTraversalRecursive.cpp](tree_problems/levelOrderTraversalRecursive.cpp), [level_order_tree_traversal_recursive.py](tree_problems/level_order_tree_traversal_recursive.py)|
|ZigZag Traversal of Tree | [zigZagTraversal.cpp](tree_problems/zigZagTraversal.cpp), [zig_zag_traversal.py](tree_problems/zig_zag_traversal.py)|
|Predecessor and Successor of a given node in Binary Search Tree | [predecessorSuccessor.cpp](tree_problems/predecessorSuccessor.cpp)|
|Given values of two nodes in a Binary Search Tree, find the Lowest Common Ancestor (LCA). Assume that both the values exist in the tree.| [lowest-common-ancestor.cpp](tree_problems/lowest-common-ancestor.cpp)|
|Given values of two nodes in a Binary Search Tree, find the Lowest Common Ancestor (LCA). Assume that both the values exist in the tree.| [lowest-common-ancestor.cpp](tree_problems/lowest-common-ancestor.cpp), [lowest_common_ancestor.py](tree_problems/lowest_common_ancestor.py)|
|Given a binary tree (unlike binary search tree), find the Lowest Common Ancestor (LCA).|[lowest-common-ancestor-binary-tree.cpp](tree_problems/lowest-common-ancestor-binary-tree.cpp)|
|Given a binary tree, print out all of its root-to-leaf paths one per line.| [printAllRootToLeafPath.cpp](tree_problems/printAllRootToLeafPath.cpp)
|Determine if a tree is sum tree. A SumTree is a Binary Tree where the value of a node is equal to sum of the nodes present in its left subtree and right subtree. An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree.| [sumTree.cpp](tree_problems/sumTree.cpp)|
Expand Down
63 changes: 63 additions & 0 deletions tree_problems/lowest_common_ancestor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Given a binary search tree, determine the lowest common ancestor of two given nodes with data
key1 and key2. Assumption is key1 and key2 exists in tree
Example
20
/ \
8 22
/ \
4 12
/ \
10 14
In the above tree, LCA of 10 and 14 is 12
similarly 4 and 22 will have LCA 20
"""

class Node:
def __init__(self, data):
"""Binary Search Tree Node representation"""
self.data = data
self.left = None
self.right = None


def LCA(root, key1, key2):
if root is None:
return None

if root.data > key1 and root.data > key2:
return LCA(root.left, key1, key2)

elif root.data < key1 and root.data < key2:
return LCA(root.right, key1, key2)

return root

def inorder(root):
if root is None:
return
inorder(root.left)
print(root.data, end =' ')
inorder(root.right)



if __name__ == "__main__":
root = Node(20)
root.right = Node(22)
root.left = Node(8)
root.left.left = Node(4)
root.left.right= Node(12)
root.left.right.right = Node(14)
root.left.right.left = Node(10)
print("Inorder traversal of the tree:")
inorder(root);
print()
print("LCA of nodes with data 10 and 14 is :")
print(LCA(root, 14, 10).data);
print("LCA of nodes with data 14 and 8 is :")
print(LCA(root, 14, 8).data);
print("LCA of root with data 4 and 22 is :")
print(LCA(root, 4, 22).data);
print("LCA of root with data 14 and 10 is :")
print(LCA(root, 10, 14).data);

0 comments on commit 81f08aa

Please sign in to comment.