-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathBoundaryTraversal.java
51 lines (50 loc) · 1.8 KB
/
BoundaryTraversal.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
/*https://practice.geeksforgeeks.org/problems/boundary-traversal-of-binary-tree/1*/
class Solution
{
ArrayList<Integer> al = new ArrayList<>();
ArrayList <Integer> printBoundary(Node node)
{
al = new ArrayList<Integer>(); //create a new array
if (node == null) //if root is null
return al; //return the empty array
al.add(node.data); //add the root
if (node.left != null) //if left is not null
preOrder(node.left); //left boundary call
inOrder(node); //leaves call
if (node.right != null) //if right is not null
postOrder(node.right); //right boundary call
return al; //return the array
}
void preOrder(Node root)
{
if (root.left != null || root.right != null) //if we didn't reach a leaf node
{
al.add(root.data); //add the node
if (root.left != null) //if left child is available
preOrder(root.left); //recursively call the left child
else //if left child is not there
preOrder(root.right); //recursively call the right child
}
}
void inOrder(Node root)
{
if (root != null) //till we traverse the end
{
inOrder(root.left); //recursively call the left subtree
if (root.left == null && root.right == null) //if the node is a leaf
al.add(root.data); //add the data
inOrder(root.right); //recursively call the right subtree
}
}
void postOrder(Node root)
{
if (root.right != null || root.left != null) // if we didn't reach a leaf node
{
if (root.right != null) //if right child is present
postOrder(root.right); //recursively call the right child
else //if right child is not present
postOrder(root.left); //recursively call the left child
al.add(root.data); //add the data
}
}
}