Skip to content

Commit 43fc7c3

Browse files
committed
Time: 58 ms (97.63%), Space: 59.6 MB (18.39%) - LeetHub
1 parent c008be3 commit 43fc7c3

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
9+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} p
12+
* @param {TreeNode} q
13+
* @return {TreeNode}
14+
*/
15+
16+
var lowestCommonAncestor = function (root, p, q) {
17+
while(root!=null) {
18+
if (root.val > p.val && root.val > q.val) {
19+
root = root.left;
20+
}
21+
else if (root.val < p.val && root.val < q.val) {
22+
root = root.right;
23+
}
24+
else {
25+
return root;
26+
}
27+
}
28+
return root;
29+
};

0 commit comments

Comments
 (0)