-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy path236-二叉树的最近公共祖先.cpp
More file actions
30 lines (30 loc) · 915 Bytes
/
236-二叉树的最近公共祖先.cpp
File metadata and controls
30 lines (30 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
static const auto __ = []()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
return nullptr;
}();
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
//如果p,q就是root,那么祖先就是root,否则递归
//如果p,q分别在左右子树,那么祖先就是root
//如果在同一颗子树,继续递归
if(root == NULL || root == p || root ==q) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left && right)
return root;
else
return left == NULL ? right : left;
}
};