-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundaryTraversal.java
More file actions
75 lines (67 loc) · 2.06 KB
/
BoundaryTraversal.java
File metadata and controls
75 lines (67 loc) · 2.06 KB
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
package BinaryTree;
import java.util.ArrayList;
//Boundary Traversal is done in 3 steps:
/*
1. Add left nodes
2. Add leaf nodes
3. Add Right nodes
*/
public class BoundaryTraversal {
static ArrayList<Integer> ans = new ArrayList<>();
public static void main(String[] args) {
TreeNode root1 = new TreeNode(1);
root1.left = new TreeNode(2);
root1.right = new TreeNode(3);
root1.right.left = new TreeNode(4);
root1.right.right = new TreeNode(5);
if(!isLeaf(root1))ans.add(root1.data);
addLeftBoundary(root1);
addLeaves(root1);
addRightBoundary(root1);
System.out.println(ans);
}
public static boolean isLeaf(TreeNode root)
{
return (root.left == null && root.right == null);
}
public static void addLeftBoundary(TreeNode root) // Step 1
{
TreeNode curr = root.left;
while (curr != null)
{
if(!isLeaf(curr))ans.add(curr.data);
if(curr.left != null){
curr = curr.left;
}else {
curr = curr.right;
}
}
}
public static void addLeaves(TreeNode root) // Step 2
{
if(isLeaf(root))
{
ans.add(root.data);
return;
}
if(root.left != null)addLeaves(root.left);
if (root.right != null) addLeaves(root.right);
}
public static void addRightBoundary(TreeNode root)//Step 3
{
/*
Now here it is frome bottom to upward direction therfore we will add the data in the list in reverse order
*/
TreeNode cur = root.right;
ArrayList < Integer > tmp = new ArrayList < Integer > ();
while (cur != null) {
if (isLeaf(cur) == false) tmp.add(cur.data);
if (cur.right != null) cur = cur.right;
else cur = cur.left;
}
int i;
for (i = tmp.size() - 1; i >= 0; --i) {
ans.add(tmp.get(i));
}
}
}