Skip to content

Commit 05600ca

Browse files
committed
Added lowestCommonAncestor solution
1 parent 12ed6eb commit 05600ca

File tree

1 file changed

+11
-0
lines changed
  • lowest-common-ancestor-of-a-binary-search-tree

1 file changed

+11
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var lowestCommonAncestor = function (root, p, q) {
2+
// Iterate if statement with comparing values
3+
while (root) {
4+
if (root.val < p.val && root.val < q.val) root = root.right;
5+
else if (root.val > p.val && root.val > q.val) root = root.left;
6+
else return root;
7+
}
8+
};
9+
10+
// TC: O(n)
11+
// SC: O(1)

0 commit comments

Comments
 (0)