-
Notifications
You must be signed in to change notification settings - Fork 692
/
ConnectSiblings.java
106 lines (79 loc) · 3.39 KB
/
ConnectSiblings.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package trees;
public class ConnectSiblings {
/*
* Given a binary tree, connect its siblings at each level
* Runtime Complexity:
* Linear, O(n)
*
* Memory Complexity:
* Constant O(1)
*
* A binary tree is a data structure made up of nodes, where each node contains, besides data, up to two children:
* "left" reference and "right" reference. Nodes with the same parent node are called siblings.
* We need to connect all siblings at each level.
* In level order traversal of a tree, all nodes are processed by depth; first the root, then the children of the
* root and so on. It is equivalent to a breadth-first-search from the root.
* Each node in the above binary tree has one more pointer i.e. next (aka sibling) along with the left and
* right pointers. 'next' is used to connect one node to the other.
*
* Given the starting node at level 'x' (where nodes at level 'x' are connected):
* 1. Traverse the nodes at level 'x' from left to right while connecting the siblings at level 'x+1'.
* Now that siblings at level 'x+1' are connected, use the head node (left-most) of level 'x+1' and
* apply the same approach on 'x+2'.
* 2.Keep running these steps till you run out of levels.
*
* */
protected static class Node {
int data;
Node left, right, nextRight;
Node(int item) {
data = item;
left = right = nextRight = null;
}
}
private Node root;
// Sets the next of root and calls connectRecur()
// for other nodes
void connect(Node p) {
// Set the next for root
p.nextRight = null;
// Set the next right for rest of the nodes (other
// than root)
connectRecur(p);
}
protected void connectRecur(Node p) {
if (p == null) return;
if (p.left != null) p.left.nextRight = p.right;
// Set the next pointer for p's right child
// p->next will be NULL if p is the right most child
// at its level
if (p.right != null)
p.right.nextRight = (p.nextRight != null) ? p.nextRight.left : null;
connectRecur(p.left);
connectRecur(p.right);
}
public static void main(String args[]) {
ConnectSiblings tree = new ConnectSiblings();
/* Constructed binary tree is
10
/ \
8 2
/
3
*/
tree.root = new Node(10);
tree.root.left = new Node(8);
tree.root.right = new Node(2);
tree.root.left.left = new Node(3);
tree.connect(tree.root);
System.out.println("Following are populated next pointers in the tree" + "(-1 is printed if there is no next)");
int a = tree.root.nextRight != null ? tree.root.nextRight.data : -1;
System.out.println("next of " + tree.root.data + " is " + a);
int b = tree.root.left.nextRight != null ? tree.root.left.nextRight.data : -1;
System.out.println("next of " + tree.root.left.data + " is " + b);
int c = tree.root.right.nextRight != null ? tree.root.right.nextRight.data : -1;
System.out.println("next of " + tree.root.right.data + " is " + c);
int d = tree.root.left.left.nextRight != null ? tree.root.left.left.nextRight.data : -1;
System.out.println("next of " + tree.root.left.left.data + " is " + d);
}
}