-
Notifications
You must be signed in to change notification settings - Fork 2
/
LCAwithParentConnectionInBinaryTree.java
85 lines (78 loc) · 2.52 KB
/
LCAwithParentConnectionInBinaryTree.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package Leetcode;
/**
* @author kalpak
*
* Given two nodes of a binary tree p and q, return their lowest common ancestor (LCA).
*
* Each node will have a reference to its parent node. The definition for Node is below:
*
* class Node {
* public int val;
* public Node left;
* public Node right;
* public Node parent;
* }
* According to the definition of LCA on Wikipedia:
* "The lowest common ancestor of two nodes p and q in a tree T is the lowest node
* that has both p and q as descendants (where we allow a node to be a descendant of itself)."
*
*
*
* Example 1:
* Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
* Output: 3
* Explanation: The LCA of nodes 5 and 1 is 3.
*
*
* Example 2:
* Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
* Output: 5
* Explanation: The LCA of nodes 5 and 4 is 5 since a node can be a descendant of itself according to the LCA definition.
*
*
* Example 3:
* Input: root = [1,2], p = 1, q = 2
* Output: 1
*
* Constraints:
*
* The number of nodes in the tree is in the range [2, 105].
* -109 <= Node.val <= 109
* All Node.val are unique.
* p != q
* p and q exist in the tree.
*
*/
public class LCAwithParentConnectionInBinaryTree {
static class NodeWithParent {
public int val;
public NodeWithParent left;
public NodeWithParent right;
public NodeWithParent parent;
NodeWithParent(int val, NodeWithParent parent) {
this.val = val;
this.parent = parent;
}
}
public static NodeWithParent lowestCommonAncestor(NodeWithParent p, NodeWithParent q) {
NodeWithParent temp1 = p;
NodeWithParent temp2 = q;
while(temp1 != temp2) {
temp1 = (temp1 == null) ? q : temp1.parent;
temp2 = (temp2 == null) ? p : temp2.parent;
}
return temp1;
}
public static void main(String[] args) {
NodeWithParent root = new NodeWithParent(3, null);
root.left = new NodeWithParent(5, root);
root.right = new NodeWithParent(1, root);
root.left.left = new NodeWithParent(6, root.left);
root.left.right = new NodeWithParent(2, root.left);
root.right.left = new NodeWithParent(0, root.right);
root.right.right = new NodeWithParent(8, root.right);
root.left.right.left = new NodeWithParent(7, root.left.right);
root.left.right.right = new NodeWithParent(4, root.left.right);
System.out.println(lowestCommonAncestor(root.left, root.left.right.right ).val);
}
}