-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1644. Lowest Common Ancestor of a Binary Tree II.cpp
48 lines (47 loc) · 1.45 KB
/
1644. Lowest Common Ancestor of a Binary Tree II.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 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:
int counter=0;
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
TreeNode* LCA= LCA_helper(root, p, q);
if(counter==2) {
return LCA;
}else {
return NULL;
}
}
TreeNode* LCA_helper(TreeNode* root, TreeNode* p, TreeNode* q){
// in V1, we instantly returned the p / q when we sees it for the base case here.
if (root==NULL) return root;
/*
why we need to recursion downward? Find the p and q !
*/
// This is for finding p and q node!!!! Not for finding the LCA !!!
TreeNode* left=LCA_helper(root->left,p,q);
TreeNode* right=LCA_helper(root->right,p,q);
// Find p or q,
// Once we found either one, we will have to update counter
if(root == p || root == q) {
counter++;
return root;
}
if(left!=NULL && right != NULL) { // left side and right side both found targets
return root;
}else if(left==NULL) {
return right;
}else {
return left;
}
}
};