-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathProblem_08.java
71 lines (66 loc) · 1.88 KB
/
Problem_08.java
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
66
67
68
69
70
71
/**
* Cracking-The-Coding-Interview
* Problem_08.java
*/
package com.deepak.ctci.Ch04_Trees_And_Graphs;
import com.deepak.ctci.Library.TreeNode;
/**
* <br> Problem Statement :
*
* Design an algorithm and write code to find the fist common
* ancestor of two nodes in a binary tree. Avoid storing additional
* nodes in a data structure.
* Note : This is not necessarily a BST
*
* </br>
*
* @author Deepak
*/
public class Problem_08 {
/**
* Method to find the common ancestor of two nodes
*
* @param root
* @param node1
* @param node2
* @return {@link TreeNode}
*/
public static <T> TreeNode<T> findCommonAncestor(TreeNode<T> root, TreeNode<T> node1, TreeNode<T> node2) {
/* If root is null or any of the given node is null, return null */
if (root == null || node1 == null || node2 == null) {
return null;
}
/* Find parent of each node and keep going up until parent matches */
TreeNode<T> parentOfNode1 = findParent(node1.data, root, null);
TreeNode<T> parentOfNode2 = findParent(node2.data, root, null);
while (parentOfNode1 != parentOfNode2) {
parentOfNode1 = findParent(parentOfNode1.data, root, null);
parentOfNode2 = findParent(parentOfNode2.data, root, null);
}
/* Return parent of any node */
return parentOfNode1;
}
/**
* Method to find the parent
*
* @param data
* @param root
* @param parent
* @return {@link TreeNode}
*/
private static <T> TreeNode<T> findParent(T data, TreeNode<T> root, TreeNode<T> parent) {
/* If root is null, no parent exists */
if (root == null) {
return null;
}
/* If root doesn't match the data, check for parent in left tree */
if (!root.data.equals(data)) {
parent = findParent(data, root.left, root);
/* If parent doesn't exists in left sub tree, check in right sub tree */
if (parent == null) {
parent = findParent(data, root.right, root);
}
}
return parent;
}
}