/*
* function TreeNode(x) {
* this.val = x;
* this.left = null;
* this.right = null;
* }
*/
/**
*
* @param root TreeNode类
* @param o1 int整型
* @param o2 int整型
* @return int整型
*/
function lowestCommonAncestor( root , o1 , o2 ) {
if(!root) return false
if(o1 == root.val || o2 == root.val) return root.val
let left = lowestCommonAncestor(root.left,o1,o2)
let right = lowestCommonAncestor(root.right,o1,o2)
if(!left) return right
if(!right) return left
return root.val
}
module.exports = {
lowestCommonAncestor : lowestCommonAncestor
};
在二叉树中找到两个结点的最近公共祖先节点
猜你喜欢
转载自blog.csdn.net/weixin_45284354/article/details/113838486
今日推荐
周排行