Skip to content

Commit ad45117

Browse files
committed
BinaryTree Inorder Traversal Added by dev-jr
1 parent f5b1764 commit ad45117

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

Programs/BinaryTreeInorder.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
//Contributed by Dev jr - https://github.com/Dev-jr-8
2+
3+
class Solution {
4+
// Function to return a list containing the inorder traversal of the tree.
5+
ArrayList<Integer> inOrder(Node root) {
6+
7+
//New ArrayList to return
8+
ArrayList<Integer> li = new ArrayList<Integer>();
9+
10+
travel(root, li);
11+
12+
return li;
13+
}
14+
15+
public static void travel(Node root, ArrayList<Integer> ans)
16+
{
17+
if(root==null) return;
18+
19+
//if there is node on left, go there
20+
if(root.left!=null)travel(root.left,ans);
21+
//add the element to list
22+
ans.add(root.data);
23+
//if there is node on right, go there
24+
if(root.right!=null)travel(root.right,ans);
25+
}
26+
}
27+
//Time Complexity : o(n)
28+
//Space Complexity : o(n)
29+
30+
//Driver Code to test the Above Class
31+
import java.util.LinkedList;
32+
import java.util.Queue;
33+
import java.io.*;
34+
import java.util.*;
35+
36+
class Node {
37+
int data;
38+
Node left;
39+
Node right;
40+
Node(int data) {
41+
this.data = data;
42+
left = null;
43+
right = null;
44+
}
45+
}
46+
47+
class GfG {
48+
49+
static Node buildTree(String str) {
50+
51+
if (str.length() == 0 || str.charAt(0) == 'N') {
52+
return null;
53+
}
54+
55+
String ip[] = str.split(" ");
56+
// Create the root of the tree
57+
Node root = new Node(Integer.parseInt(ip[0]));
58+
// Push the root to the queue
59+
60+
Queue<Node> queue = new LinkedList<>();
61+
62+
queue.add(root);
63+
// Starting from the second element
64+
65+
int i = 1;
66+
while (queue.size() > 0 && i < ip.length) {
67+
68+
// Get and remove the front of the queue
69+
Node currNode = queue.peek();
70+
queue.remove();
71+
72+
// Get the current node's value from the string
73+
String currVal = ip[i];
74+
75+
// If the left child is not null
76+
if (!currVal.equals("N")) {
77+
78+
// Create the left child for the current node
79+
currNode.left = new Node(Integer.parseInt(currVal));
80+
// Push it to the queue
81+
queue.add(currNode.left);
82+
}
83+
84+
// For the right child
85+
i++;
86+
if (i >= ip.length) break;
87+
88+
currVal = ip[i];
89+
90+
// If the right child is not null
91+
if (!currVal.equals("N")) {
92+
93+
// Create the right child for the current node
94+
currNode.right = new Node(Integer.parseInt(currVal));
95+
96+
// Push it to the queue
97+
queue.add(currNode.right);
98+
}
99+
i++;
100+
}
101+
102+
return root;
103+
}
104+
105+
public static void main(String[] args) throws IOException {
106+
BufferedReader br =
107+
new BufferedReader(new InputStreamReader(System.in));
108+
109+
int t = Integer.parseInt(br.readLine());
110+
111+
while (t > 0) {
112+
String s = br.readLine();
113+
Node root = buildTree(s);
114+
Solution g = new Solution();
115+
ArrayList<Integer> res = g.inOrder(root);
116+
for (int i = 0; i < res.size(); i++)
117+
System.out.print(res.get(i) + " ");
118+
System.out.print("\n");
119+
t--;
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)