Skip to content

236. Lowest Common Ancestor of a Binary Tree #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ continually updating 😃.
* [208. Implement Trie (Prefix Tree)](src/0208_implement_trie_prefix_tree/impltrie.go)   *`trie`*
* [226. Invert Binary Tree](src/0226_invert_binary_tree/invert_binary_tree.go)   *`binary tree`*
* [235. Lowest Common Ancestor of a Binary Search Tree](src/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)   *`binary tree`*
* [236. Lowest Common Ancestor of a Binary Tree](src/0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca.go)   *`binary tree`*
* [257. Binary Tree Paths](src/0257_binary_tree_paths/binary_tree_paths.go)   *`binary tree`*
* [307. Range Sum Query - Mutable](src/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go)   *`segment tree`*
* [404. Sum of Left Leaves](src/0404_sum_of_left_leaves/sum_of_left_leaves.go)   *`binary tree`*
Expand Down
30 changes: 30 additions & 0 deletions src/0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
236. Lowest Common Ancestor of a Binary Tree
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
*/
// 2019-02-21

package lca

// TreeNode Definition for TreeNode.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
if root == nil || root == p || root == q {
return root
}
left := lowestCommonAncestor(root.Left, p, q)
right := lowestCommonAncestor(root.Right, p, q)

if left != nil && right != nil {
return root
}
if left != nil {
return left
}
return right
}
39 changes: 39 additions & 0 deletions src/0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lca

import "testing"

func TestLCA(t *testing.T) {
type arg struct {
root, p, q *TreeNode
}

tree := createBinaryTree([]int{6, 2, 8, 0, 4, 7, 9, -1, 1, 3, 5})

testCases := []arg{
{root: tree, p: tree.Left.Right.Left, q: tree.Left.Right.Right},
{p: &TreeNode{Val: 2}, q: &TreeNode{Val: 8}},
}
expected := []*TreeNode{{Val: 4}, nil}

for index, data := range testCases {
if res := lowestCommonAncestor(data.root, data.p, data.q); res != nil && res.Val != expected[index].Val {
t.Errorf("expected %v, got %v", expected[index], res)
} else if res == nil && res != expected[index] {
t.Errorf("expected %v, got %v", expected[index], res)
}
}
}

func createBinaryTree(nums []int) *TreeNode {
return performCreate(nums, 0)
}

func performCreate(nums []int, index int) *TreeNode {
if index >= len(nums) {
return nil
}
root := &TreeNode{Val: nums[index]}
root.Left = performCreate(nums, 2*index+1)
root.Right = performCreate(nums, 2*index+2)
return root
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
|0219|[219. Contains Duplicate II](0219_contains_duplicate_2/contains_duplicate_2.go)|Easy|*`map`*|
|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|
|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`*|
|0236|[236. Lowest Common Ancestor of a Binary Tree](0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca.go)|Medium|*`recursion; `* *`binary tree`*|
|0237|[237. Delete Node in a Linked List](0237_delete_node_in_a_linked_list/dniall.go)|Easy|*`linked list`*|
|0257|[257. Binary Tree Paths](0257_binary_tree_paths/binary_tree_paths.go)|Easy|*`binary tree`*|
|0258|[258. Add Digits](0258_add_digits/add_digits.go)|Easy|*`math`*|
Expand Down