mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 12:15:58 +08:00
Update 0235.二叉搜索树的最近公共祖先.md
可以写的更通俗易懂一些
This commit is contained in:
@ -328,13 +328,11 @@ var lowestCommonAncestor = function(root, p, q) {
|
|||||||
}
|
}
|
||||||
if(root.val>p.val&&root.val>q.val) {
|
if(root.val>p.val&&root.val>q.val) {
|
||||||
// 向左子树查询
|
// 向左子树查询
|
||||||
let left = lowestCommonAncestor(root.left,p,q);
|
return root.left = lowestCommonAncestor(root.left,p,q);
|
||||||
return left !== null&&left;
|
|
||||||
}
|
}
|
||||||
if(root.val<p.val&&root.val<q.val) {
|
if(root.val<p.val&&root.val<q.val) {
|
||||||
// 向右子树查询
|
// 向右子树查询
|
||||||
let right = lowestCommonAncestor(root.right,p,q);
|
return root.right = lowestCommonAncestor(root.right,p,q);
|
||||||
return right !== null&&right;
|
|
||||||
}
|
}
|
||||||
return root;
|
return root;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user