题目:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==p||root==q||root==nullptr) return root; //如果当前节点为空或者当前节点即为其中某个指定节点
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left&&right) return root;
if(!left) return right;
return left;
}
};
以上代码转自代码随想录
标签:lowestCommonAncestor,right,TreeNode,Offer,II,二叉树,return,root,left From: https://www.cnblogs.com/fly-smart/p/17659407.html