一、题目地址
https://www.nowcoder.com/practice/e0cc33a83afe4530bcec46eba3325116?tpId=188&&tqId=38564&rp=1&ru=/activity/oj&qru=/ta/job-code-high-week/question-ranking
二、具体代码
function lowestCommonAncestor( root , o1 , o2 ) {
if(root === null) {
return null;
}
if(root.val === o1 || root.val === o2) {
return root.val;
}
let left = lowestCommonAncestor( root.left , o1 , o2 );
let right = lowestCommonAncestor( root.right , o1 , o2 );
if(left !== null && right !== null) {
return root.val;
}
return left !== null ? left : right;
}
module.exports = {
lowestCommonAncestor : lowestCommonAncestor
};