Skip to content

Commit 14f266f

Browse files
committed
Add code for iterative tree
1 parent f7d23e8 commit 14f266f

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package algorithm.basic.iterativetree;
2+
3+
/**
4+
* You are given a doubly linked list which in addition to the next and previous pointers,
5+
* it could have a child pointer, which may or may not point to a separate doubly linked list.
6+
* These child lists may have one or more children of their own, and so on.
7+
*
8+
* Flatten the list so that all the nodes appear in a single-level, doubly linked list.
9+
* You are given the head of the first level of the list.
10+
*/
11+
public class FlattenMultiLevelList {
12+
public Node flatten(Node head) {
13+
Node cur = head;
14+
while (cur != null) {
15+
// case 1: no child, no need to flatten child list
16+
if (cur.child == null) {
17+
cur = cur.next;
18+
continue;
19+
}
20+
// case 2: has child, find tail of child list, connect to next
21+
Node tail = cur.child;
22+
while (tail.next != null) {
23+
tail = tail.next;
24+
}
25+
// connect with the child list
26+
tail.next = cur.next;
27+
if (cur.next != null) { // avoid NPE, cur maybe the last node
28+
cur.next.prev = tail;
29+
}
30+
cur.next = cur.child;
31+
cur.child.prev = cur;
32+
cur.child = null; // delink
33+
// traverse next node
34+
cur = cur.next;
35+
}
36+
return head;
37+
}
38+
39+
private static class Node {
40+
int val;
41+
Node next;
42+
Node prev;
43+
Node child;
44+
}
45+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package algorithm.basic.iterativetree;
2+
3+
import datastrcture.*;
4+
5+
/**
6+
* Given a binary tree, flatten it to a linked list in-place.
7+
*/
8+
9+
public class FlattenTreeToList {
10+
public void flatten(TreeNode root) {
11+
if (root == null) { // base case
12+
return;
13+
}
14+
TreeNode cur = root;
15+
while (cur != null) { // traverse down along the rightmost path
16+
TreeNode next = cur.right; // store next node
17+
// flatten the left subtree to insert between cur and cur.right
18+
if (cur.left != null) {
19+
// find the tail of the right subtree of cur.left
20+
TreeNode tail = cur.left;
21+
while (tail.right != null) {
22+
tail = tail.right;
23+
}
24+
// connect the tail to stored next
25+
tail.right = next;
26+
cur.right = cur.left;
27+
cur.left = null; // delink
28+
}
29+
cur = cur.right;
30+
}
31+
}
32+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package algorithm.basic.iterativetree;
2+
3+
/**
4+
* You are given a perfect binary tree where all leaves are on the same level, and every parent has two children.
5+
* The binary tree has the following definition:
6+
* class Node {
7+
* int val;
8+
* Node left;
9+
* Node right;
10+
* Node next;
11+
* }
12+
*
13+
*
14+
* Populate each next pointer to point to its next right node.
15+
* If there is no next right node, the next pointer should be set to NULL.
16+
*
17+
* Initially, all next pointers are set to NULL.
18+
*/
19+
public class PopulateNextRight {
20+
public Node connect(Node root) {
21+
if (root == null) {
22+
return root;
23+
}
24+
Node leftMost = root;
25+
while (leftMost.left != null) {
26+
// start from the leftmost node, connect current level
27+
Node cur = leftMost;
28+
while (cur != null) { // traverse the linked list of current level
29+
// connect for same root nodes
30+
cur.left.next = cur.right;
31+
// connect for inter-root nodes
32+
if (cur.next != null) { // avoid NPE
33+
cur.right.next = cur.next.left;
34+
}
35+
cur = cur.next;
36+
}
37+
leftMost = leftMost.left;
38+
// traverse the left most path to arrive last level
39+
}
40+
return root;
41+
}
42+
43+
private static class Node {
44+
int val;
45+
Node left;
46+
Node right;
47+
Node next;
48+
}
49+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package algorithm.basic.iterativetree;
2+
3+
/**
4+
* You are given a tree:
5+
* class Node {
6+
* int val;
7+
* Node left;
8+
* Node right;
9+
* Node next;
10+
* }
11+
*
12+
*
13+
* Populate each next pointer to point to its next right node.
14+
* If there is no next right node, the next pointer should be set to NULL.
15+
*
16+
* Initially, all next pointers are set to NULL.
17+
*/
18+
public class PopulateNextRight2 {
19+
public Node connect2(Node root) {
20+
if (root == null) {
21+
return null;
22+
}
23+
Node leftMost = root;
24+
while (leftMost != null) {
25+
// start from leftMost node to traverse current level
26+
// and connect the next level
27+
Node cur = leftMost; // current node at current level
28+
Node dummy = new Node(0); // used to store head of next level
29+
Node prev = dummy; // the leading node of next level
30+
// use dummy to avoid null check to simplify the implementation
31+
while (cur != null) {
32+
// traverse current level to connect next level node
33+
// populate prev.next to next node in the next level
34+
if (cur.left != null) {
35+
prev.next = cur.left;
36+
prev = prev.next;
37+
}
38+
if (cur.right != null) {
39+
prev.next = cur.right;
40+
prev = prev.next;
41+
}
42+
// move to next node in current level
43+
cur = cur.next;
44+
}
45+
// move leftMost to left most node (head) of next level
46+
leftMost = dummy.next;
47+
}
48+
return root;
49+
}
50+
51+
private static class Node {
52+
int val;
53+
Node left;
54+
Node right;
55+
Node next;
56+
57+
public Node(int val) {
58+
this.val = val;
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)