Skip to content

Commit c3542e7

Browse files
authored
235. Lowest Common Ancestor of a Binary Search Tree (#56)
* 235 solved. * fix ut
1 parent 1214c29 commit c3542e7

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ continually updating 😃.
103103
* [112. Path Sum](./src/0112_path_sum/path_sum.go)   *`binary tree;`*   *`dfs`*
104104
* [144. Binary Tree Preorder Traversal](src/0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)   *`binary tree;`*   *`pre-order traversal`*
105105
* [226. Invert Binary Tree](./src/0226_invert_binary_tree/invert_binary_tree.go)   *`binary tree;`*
106+
* [235. Lowest Common Ancestor of a Binary Search Tree](src/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)   *`binary tree;`*
106107

107108
### Binary Search
108109
* [33. Search in Rotated Sorted Array](./src/0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)   *`array;`*  *`binary search`*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
235. Lowest Common Ancestor of a Binary Search Tree
3+
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
4+
5+
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
6+
7+
According to the definition of LCA on Wikipedia:
8+
“The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has
9+
both p and q as descendants (where we allow a node to be a descendant of itself).”
10+
11+
Note:
12+
All of the nodes' values will be unique.
13+
p and q are different and both values will exist in the BST.
14+
*/
15+
// time: 2019-01-07
16+
17+
package lcaoabst
18+
19+
// TreeNode Definition for TreeNode.
20+
type TreeNode struct {
21+
Val int
22+
Left *TreeNode
23+
Right *TreeNode
24+
}
25+
26+
// recursive
27+
// time complexity: O(log n), where n is the nodes number of the tree.
28+
// space complexity: O(h), where h is the height of the tree.
29+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
30+
if root == nil {
31+
return root
32+
}
33+
34+
if root.Val > p.Val && root.Val > q.Val {
35+
return lowestCommonAncestor(root.Left, p, q)
36+
}
37+
38+
if root.Val < p.Val && root.Val < q.Val {
39+
return lowestCommonAncestor(root.Right, p, q)
40+
}
41+
return root
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lcaoabst
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLowestCommonAncestor(t *testing.T) {
8+
type arg struct {
9+
root, p, q *TreeNode
10+
}
11+
12+
testCases := []arg{
13+
{root: createBinaryTree([]int{6, 2, 8, 0, 4, 7, 9, -1, 1, 3, 5}), p: &TreeNode{Val: 3}, q: &TreeNode{Val: 5}},
14+
{p: &TreeNode{Val: 2}, q: &TreeNode{Val: 8}},
15+
}
16+
expected := []*TreeNode{{Val: 4}, nil}
17+
18+
for index, data := range testCases {
19+
if res := lowestCommonAncestor(data.root, data.p, data.q); res != nil && res.Val != expected[index].Val {
20+
t.Errorf("expected %v, got %v", expected[index], res)
21+
} else if res == nil && res != expected[index] {
22+
t.Errorf("expected %v, got %v", expected[index], res)
23+
}
24+
}
25+
}
26+
27+
func createBinaryTree(nums []int) *TreeNode {
28+
return performCreate(nums, 0)
29+
}
30+
31+
func performCreate(nums []int, index int) *TreeNode {
32+
if index >= len(nums) {
33+
return nil
34+
}
35+
root := &TreeNode{Val: nums[index]}
36+
root.Left = performCreate(nums, 2*index+1)
37+
root.Right = performCreate(nums, 2*index+2)
38+
return root
39+
}

src/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
|0217|[217. Contains Duplicate](0217_contains_duplicate/contains_duplicate.go)|Easy|*`map`*|
7272
|0219|[219. Contains Duplicate II](0219_contains_duplicate_2/contains_duplicate_2.go)|Easy|*`map`*|
7373
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|
74+
|0235|[235. Lowest Common Ancestor of a Binary Search Tree](0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)|Easy|*`recursion; `* *`binary tree`*|
7475
|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/> [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|
7576
|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|
7677
|0303|[303. Range Sum Query - Immutable](0303_range_sum_query/rsqim.go)|Easy||

0 commit comments

Comments
 (0)