@@ -25,16 +25,79 @@ Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(L
2525Binary Tree LintCode Copyright
2626
2727
28+ */
29+
30+ /*
31+ Thoughts:
32+ Revisit this on 12.11.2015.
33+ To correctly understand this approach when there is not 'parent' atribute available in node.
34+
35+ We divide and coquer (in this case DFS) into 2 branches, and we are actually asking each node to check:
36+ Do I have a leaf child of nodeA (could be futher down in the tree)?
37+ Do I have a leaf child of nodeB (could be futher down in the tree)?
38+ 1. If I have leaf child of A && B, then i'm the deepest parent! Return.
39+ 2. If I only have A, or B: mark myself as an ancestor of A or B.
40+ 3. If I don't have leaf child of A nor B, I'm not an ancestor, failed, return null.
41+
42+ After the common ancestor is found at any deep level, and returned itself to parent level,
43+ we can assume other branches must be null (because they are not ancestor, since we are),
44+ then the this common ancestor node will be passed to highest level.
45+
46+ However, with one problem:
47+ When review the problem, calling the recursive functions of the 'lowestCommonAncestor' is just
48+ confusing. It's not easy to see the relationship between leef child and ancestor candidates.
49+
50+
51+ */
52+ public class Solution {
53+ /**
54+ * @param root: The root of the binary search tree.
55+ * @param A and B: two nodes in a Binary.
56+ * @return: Return the least common ancestor(LCA) of the two nodes.
57+ */
58+ public TreeNode lowestCommonAncestor (TreeNode root , TreeNode A , TreeNode B ) {
59+ if (root == null || root == A || root == B ) {
60+ return root ;
61+ }
62+ TreeNode left = lowestCommonAncestor (root .left , A , B );
63+ TreeNode right = lowestCommonAncestor (root .right , A , B );
64+
65+ if (left != null && right != null ) {//Found both A leaf and B leaf
66+ return root ;
67+ } else if (left != null || right != null ) {
68+ return left != null ? left : right ;
69+ } else {
70+ return null ;
71+ }
72+ }
73+ }
74+
75+ /*
76+ //Another way using regular way: see solution:
77+ This is for the case that each node can reach its higher level parent....
78+ Basically put all parents into a list,
79+ [root, next level, next level... parent, nodeA]
80+ [root, next level, next level... parent, nodeB]
81+ compare these 2 lists see when to have a different node. that node.parent is the parent
82+
83+ Or, if the list is identical, then their parent might just be same right-above-level parent.
84+
85+ http://www.ninechapter.com/solutions/lowest-common-ancestor/
86+ */
87+
88+
89+ /*
90+ Older same version
2891Think process:
2992Divide and conquer:
3093start from root, divide into 2 ways …
3194At the bottom, if it’s node1 or node2, send back.
32- If any line that’s not having node1 or node2 as leaf, they will become null.
33- At the ancestor spot: because node1!=null and node2!=null, the ancestor return itself.
34- From this point, any level higher, it will return the ancestor because the other child-line has been null at the time.
95+ 1. If any line that’s not having node1 or node2 as leaf, they will become null.
96+ 2. At the ancestor spot: because node1!=null and node2!=null, the ancestor return itself.
97+ 3. From this point, any level higher, it will return the ancestor because the other child-line has been null at the time.
3598When it returns to the top, return solution : ancestor
36- */
3799
100+ */
38101/**
39102 * Definition of TreeNode:
40103 * public class TreeNode {
@@ -70,7 +133,3 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) {
70133 }
71134 }
72135}
73- /*
74- //Another way using regular way: see solution:
75- http://www.ninechapter.com/solutions/lowest-common-ancestor/
76- */
0 commit comments