forked from jvm-coder/Hacktoberfest2022_aakash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundary_Traversal.java
More file actions
66 lines (62 loc) · 1.92 KB
/
Boundary_Traversal.java
File metadata and controls
66 lines (62 loc) · 1.92 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
import java.util.* ;
import java.io.*;
/************************************************************
Following is the Binary Tree node structure:
class TreeNode {
int data;
TreeNode left;
TreeNode right;
TreeNode(int data) {
this.data = data;
this.left = null;
this.right = null;
}
}
************************************************************/
import java.util.ArrayList;
public class Solution {
public static boolean isLeaf(TreeNode node){
if(node.left==null && node.right==null){
return true;
}
return false;
}
public static void leftboundary(TreeNode node,ArrayList<Integer> res){
TreeNode curr = node.left;
while(curr!=null){
if(!isLeaf(curr)) res.add(curr.data);
if(curr.left!=null) curr =curr.left;
else curr = curr.right;
}
}
public static void rightboundary(TreeNode node,ArrayList<Integer> res){
ArrayList<Integer> temp = new ArrayList<>();
TreeNode curr = node.right;
while(curr!=null){
if(!isLeaf(curr)) temp.add(curr.data);
if(curr.right!=null) curr =curr.right;
else curr = curr.left;
}
for(int i=temp.size()-1;i>=0;i--){
res.add(temp.get(i));
}
}
public static void addleaf(TreeNode node,ArrayList<Integer> res){
if(isLeaf(node)){
res.add(node.data);
return;
}
if(node.left!=null) addleaf(node.left,res);
if(node.right!=null) addleaf(node.right,res);
}
public static ArrayList<Integer> traverseBoundary(TreeNode root){
// Write your code here.
ArrayList<Integer> res = new ArrayList<>();
if(root==null) return res;
if(!isLeaf(root)) res.add(root.data);
leftboundary(root,res);
addleaf(root,res);
rightboundary(root,res);
return res;
}
}