-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoundary_of_Binary_Tree.java
123 lines (100 loc) · 2.94 KB
/
Boundary_of_Binary_Tree.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Indian Army setup some military-camps, sitauted at random places at LAC in Galwan.
// There exist a main base camp connected with other base camps as follows:
// Each military-camp is connected with atmost two other military-camps.
// Each military-camp will be identified with an unique ID,(an integer).
// To safeguard all the military-camps, Govt of India planned to setup protective
// S.H.I.E.L.D. Govt of India ask your help to build the S.H.I.E.L.D that should
// enclose all the militar-camps.
// You are given the IDs of the military-camps as binary tree.
// Your task is to find and return the military camp IDs, those are on the edge of
// the S.H.I.E.L.D in anti-clockwise order.
// Implement the class Solution:
// 1. public List<Integer> compoundWall(BinaryTreeNode root): returns a list.
// NOTE:
// '-1' in the input IDs indicates no military-camp (NULL).
// Input Format:
// -------------
// space separated integers, military-camp IDs.
// Output Format:
// --------------
// Print all the military-camp IDs, which are at the edge of S.H.I.E.L.D.
// Sample Input-1:
// ---------------
// 5 2 4 7 9 8 1
// Sample Output-1:
// ----------------
// [5, 2, 7, 9, 8, 1, 4]
// Sample Input-2:
// ---------------
// 11 2 13 4 25 6 -1 -1 -1 7 18 9 10
// Sample Output-2:
// ----------------
// [11, 2, 4, 7, 18, 9, 10, 6, 13]
import java.util.*;
/*
//TreeNode Structure for Your Reference..
class Node{
public int data;
public Node left, right;
public Node(int data){
this.data = data;
left = null;
right = null;
}
}
*/
class Solution {
static List<Integer> lst=new ArrayList<>();
static List<Integer> temp=new ArrayList<>();
public List<Integer> compoundWall(Node root) {
// Implement Your Code here..
lst.add(root.data);
printleft(root.left);
printleaves(root);
printright(root.right);
for(int i=temp.size()-1;i>=0;i--){
lst.add(temp.get(i));
}
return lst;
}
public static void printleft(Node root){
if(root==null){
return;
}
if(root.left==null && root.right==null){
return;
}
lst.add(root.data);
if(root.left!=null){
printleft(root.left);
}
else if(root.right!=null){
printleft(root.right);
}
}
public static void printright(Node root){
if(root==null){
return;
}
if(root.left==null && root.right==null){
return;
}
temp.add(root.data);
if(root.right!=null){
printright(root.right);
}
else if(root.left!=null){
printright(root.left);
}
}
public static void printleaves(Node root){
if(root==null){
return;
}
if(root.left==null && root.right==null){
lst.add(root.data);
}
printleaves(root.left);
printleaves(root.right);
}
}