-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlca_binarytree.py
65 lines (45 loc) · 1.99 KB
/
lca_binarytree.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# recursive
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
if not root or root == p or root == q: # find p or q
return root
pExists = self.lowestCommonAncestor(root.left, p, q)
qExists = self.lowestCommonAncestor(root.right, p, q)
if pExists and qExists: # both p and q lie in different subtrees
return root
return pExists if pExists else qExists # either you are having p == q or p and q(which are different) lie in same subtree
# Another idea : Collect paths of p and q from the root and save them in aux array. Now find a node that's not common and return the node previous to the uncommon node(which by definition is common)
# bool tracePath(TreeNode* root, TreeNode* p, vector<TreeNode*> &path){
# if(root == NULL)
# return false;
# if(root == p){
# path.push_back(p);
# return true;
# }
# path.push_back(root);
# if(tracePath(root->left, p, path)){
# return true;
# }
# else if(tracePath(root->right, p, path)){
# return true;
# }
# path.pop_back();
# return false;
# }
# TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
# vector<TreeNode*> pathOfP,pathOfQ;
# tracePath(root, p, pathOfP);
# tracePath(root, q, pathOfQ);
# int i;
# for(i=0; i<pathOfP.size() && i<pathOfQ.size(); i++){
# if(pathOfP[i] != pathOfQ[i])
# break;
# }
# return pathOfP[i-1];
# }