Leetcode:面试题68 - II. 二叉树的最近公共祖先
Leetcode:面试题68 - II. 二叉树的最近公共祖先
Talk is cheap . Show me the code .
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root==NULL||p==root||q==root) return root;
TreeNode *left,*right;
left=lowestCommonAncestor(root->left,p,q);
right=lowestCommonAncestor(root->right,p,q);
if(left==NULL) return right;
if(right==NULL) return left;
return root;
}
};