Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 11fb1ef

Browse files
authored
236 solved🖖. (keep-practicing#76)
1 parent a1c451d commit 11fb1ef

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ continually updating 😃.
116116
* [226. Invert Binary Tree](src/0226_invert_binary_tree/invert_binary_tree.go)   *`binary tree`*
117117
* [211. Add and Search Word - Data structure design](src/0211_add_and_search_word/add_and_search_word.go)   *`trie`*
118118
* [235. Lowest Common Ancestor of a Binary Search Tree](src/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)   *`binary tree`*
119+
* [236. Lowest Common Ancestor of a Binary Tree](src/0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca.go)   *`binary tree`*
119120
* [257. Binary Tree Paths](src/0257_binary_tree_paths/binary_tree_paths.go)   *`binary tree`*
120121
* [307. Range Sum Query - Mutable](src/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go)   *`segment tree`*
121122
* [404. Sum of Left Leaves](src/0404_sum_of_left_leaves/sum_of_left_leaves.go)   *`binary tree`*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
236. Lowest Common Ancestor of a Binary Tree
3+
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
4+
*/
5+
// 2019年02月21日
6+
7+
package lca
8+
9+
// TreeNode Definition for TreeNode.
10+
type TreeNode struct {
11+
Val int
12+
Left *TreeNode
13+
Right *TreeNode
14+
}
15+
16+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
17+
if root == nil || root == p || root == q {
18+
return root
19+
}
20+
left := lowestCommonAncestor(root.Left, p, q)
21+
right := lowestCommonAncestor(root.Right, p, q)
22+
23+
if left != nil && right != nil {
24+
return root
25+
}
26+
if left != nil {
27+
return left
28+
}
29+
return right
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package lca
2+
3+
import "testing"
4+
5+
func TestLCA(t *testing.T) {
6+
type arg struct {
7+
root, p, q *TreeNode
8+
}
9+
10+
tree := createBinaryTree([]int{6, 2, 8, 0, 4, 7, 9, -1, 1, 3, 5})
11+
12+
testCases := []arg{
13+
{root: tree, p: tree.Left.Right.Left, q: tree.Left.Right.Right},
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
|0219|[219. Contains Duplicate II](0219_contains_duplicate_2/contains_duplicate_2.go)|Easy|*`map`*|
7878
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|
7979
|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`*|
80+
|0236|[236. Lowest Common Ancestor of a Binary Tree](0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca.go)|Medium|*`recursion; `* *`binary tree`*|
8081
|0237|[237. Delete Node in a Linked List](0237_delete_node_in_a_linked_list/dniall.go)|Easy|*`linked list`*|
8182
|0257|[257. Binary Tree Paths](0257_binary_tree_paths/binary_tree_paths.go)|Easy|*`binary tree`*|
8283
|0258|[258. Add Digits](0258_add_digits/add_digits.go)|Easy|*`math`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /