Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84c941b

Browse files
committed
GO: 235. Lowest Common Ancestor of a BST
1 parent 8a8654d commit 84c941b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
10+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
11+
if p.Val > root.Val && q.Val > root.Val {
12+
return lowestCommonAncestor(root.Right, p, q)
13+
}
14+
15+
if p.Val < root.Val && q.Val < root.Val {
16+
return lowestCommonAncestor(root.Left, p, q)
17+
}
18+
19+
return root
20+
}

0 commit comments

Comments
 (0)