題目描述:
https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/
思路:
代碼實現:
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(!root || p == root || q == root)
return root;
TreeNode* left = lowestCommonAncestor(root->left,p,q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left && right)
return root;
else return left? left:right;
}
};