-
Notifications
You must be signed in to change notification settings - Fork 6
/
FlattenBinaryTree.java
89 lines (71 loc) · 1.99 KB
/
FlattenBinaryTree.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
import java.util.Stack;
/**
* *******************************************************************
* Given a binary tree, flatten it to a linked list in-place.
*
* @author achoudhary
* @version 1.0.0
********************************************************************
*/
class TreeNode{
int value ;
TreeNode left;
TreeNode right;
TreeNode(int i){
this.value = i;
}
}
public class FlattenBinaryTree
{
/**
* recursive method
*
* we need to make sure always keep all the values to the right node,
* so make everything in left as null after assigning the value to right of root node
*
* callig the flatten simply assigns the value to root, as its not going to create any trouble
* dont assign to left or right
*
* @param root
* @return
*/
public TreeNode flatten(TreeNode root){
if(root == null)
return root;
TreeNode rTree = root.right;
if(root.left != null){
root.right = root.left;
root.left = null;
root = flatten(root.right);
}
if(rTree != null){
root.left = rTree;
root = flatten(rTree.right);
}
return root;
}
/**
* This approach is with stack where we simply add always the right value of the node to stack and
* read later but add left value always directly to a new tree created t.
*
* Later if stack is not empty means it has series of right node, pop one after one
*
* @param root
*/
public void flattenStack(TreeNode root){
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode t = root;
while(t != null || !stack.empty()){
if(t.right != null){
stack.push(t.right);
}
if(t.left != null){
t.right = t.left;
t.left = null;
}else if(!stack.empty()){
t.right = stack.pop();
}
t = t.right;
}
}
}