AcWing 88. 树中两个结点的最低公共祖先
原题链接
中等
作者:
cyb-包子
,
2021-07-19 16:15:53
,
所有人可见
,
阅读 209
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {TreeNode} p
* @param {TreeNode} q
* @return {TreeNode}
*/
var lowestCommonAncestor = function(root, p, q) {
if(root === null || root === p || root === q){
return root;
}
let l = lowestCommonAncestor(root.left, p, q);
let r = lowestCommonAncestor(root.right, p, q);
if(!l) return r;
if(!r) return l;
return root;
};