Skip to content

Commit 6e134be

Browse files
committed
refactor Lowest Common Ancestor of a Binary Search Tree
1 parent ef711b7 commit 6e134be

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

go/lowest_common_ancester_of_a_binary_search_tree.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ package main
44
func lowestCommonAncestorIterative(root, p, q *TreeNode) *TreeNode {
55
node := root
66
for node != nil {
7-
if p.Val <= node.Val && node.Val <= q.Val || q.Val <= node.Val && node.Val <= p.Val {
8-
return node
9-
}
107
if p.Val < node.Val && q.Val < node.Val {
118
node = node.Left
9+
continue
1210
}
1311
if node.Val < p.Val && node.Val < q.Val {
1412
node = node.Right
13+
continue
1514
}
15+
return node
1616
}
1717
return nil
1818
}

0 commit comments

Comments
 (0)