Skip to content

Commit bd64387

Browse files
solves lowest common ancestor in bst
1 parent e081cf9 commit bd64387

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
| 231 | [Power of Two](https://leetcode.com/problems/power-of-two) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PowerOf2.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/is_power_of_2.py) |
7171
| 232 | [Implement Queue Using Stacks](https://leetcode.com/problems/implement-queue-using-stacks) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MyQueue.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/implement_queue_using_stacks.py) |
7272
| 234 | [Palindrome Linked Lists](https://leetcode.com/problems/palindrome-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PalindromeLinkedList.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/palindrome_linked_list.py) |
73-
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LowestCommonAncestorOfBinarySearchTree.java) |
73+
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LowestCommonAncestorOfBinarySearchTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/lowest_common_ancestor_of_bst.py) |
7474
| 237 | [Delete a Node In A Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/DeleteANodeInLinkedList.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/delete_node_in_linked_list.py) |
7575
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ValidAnagram.java) |
7676
| 243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance) | Easy | |
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from collections import deque
2+
3+
4+
# Definition for a binary tree node.
5+
class TreeNode:
6+
def __init__(self, x):
7+
self.val = x
8+
self.left = None
9+
self.right = None
10+
11+
def __repr__(self):
12+
return str(self.val)
13+
14+
def __str__(self):
15+
return str(self.val)
16+
17+
18+
class Solution:
19+
def get_path_to(self, root: TreeNode, node: TreeNode) -> deque:
20+
path = deque()
21+
while root.val != node.val:
22+
path.append(root)
23+
root = root.left if root.val > node.val else root.right
24+
path.append(root)
25+
return path
26+
27+
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
28+
path_p = self.get_path_to(root, p)
29+
path_q = self.get_path_to(root, q)
30+
index = 0
31+
for node_p, node_q in zip(path_p, path_q):
32+
if node_p is not node_q:
33+
break
34+
index += 1
35+
return path_p[index - 1]

0 commit comments

Comments
 (0)