Skip to content

Commit 431814b

Browse files
committed
Traversals and Depth of Binary Tree
1 parent a4f8a62 commit 431814b

File tree

4 files changed

+223
-8
lines changed

4 files changed

+223
-8
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Problems based on the Tree data structure
1717
|| [Iterative Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Approach](#iterative-preorder-traversal) | [Java Soultion](./src/sde_sheet/.java) |
1818
|| [Iterative Postorder Traversal (Using 2 stacks)](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Approach](#using-2-stacks) | [Java Soultion](./src/sde_sheet/.java) |
1919
|| [Iterative Postorder Traversal (Using 1 stack)](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Approach](#using-1-stack) | [Java Soultion](./src/sde_sheet/.java) |
20-
| 🔃 | [All Traversals in one code](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Approach](#) | [Java Soultion](./src/sde_sheet/.java) |
20+
| 🔃 | [All Traversals in one code](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Approach](#preorder-inorder-postorder-traversals-in-one-traversal) | [Java Soultion](./src/sde_sheet/.java) |
2121
| 🔃 | [Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view/) | [Brute, Better & Optimal Approaches](#) | [Java Soultion](./src/sde_sheet/.java) |
2222
| 🔃 | [Bottom View of Binary Tree](https://practice.geeksforgeeks.org/problems/bottom-view-of-binary-tree/1#) | [Brute, Better & Optimal Approaches](#) | [Java Soultion](./src/sde_sheet/.java) |
2323
| 🔃 | [Top View of Binary Tree](https://practice.geeksforgeeks.org/problems/top-view-of-binary-tree/1) | [Brute, Better & Optimal Approaches](#) | [Java Soultion](./src/sde_sheet/.java) |
@@ -254,6 +254,10 @@ Problems based on the Tree data structure
254254

255255
<img src="assets/Iterative postorder traversal using 1 stack dry run 2.jpg" alt="Iterative postorder traversal using 1 stack dry run 2" width="400px" />
256256

257+
## Preorder Inorder Postorder Traversals in One Traversal
258+
259+
-
260+
257261

258262
---
259263
---

src/dsa_one/HeightOfBinaryTree.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66

77
public class HeightOfBinaryTree {
88

9+
public static int heightOfBinaryTree(Node root) {
10+
if (root == null)
11+
return 0;
12+
13+
// Here we take the maximum height of left subtree and right subtree
14+
// Then we add one for counting the root for the height
15+
return Math.max(heightOfBinaryTree(root.left), heightOfBinaryTree(root.right)) + 1;
16+
}
17+
918
static Scanner sc = null;
1019

1120
public static void main(String[] args) {
@@ -18,13 +27,6 @@ public static void main(String[] args) {
1827

1928
}
2029

21-
public static int heightOfBinaryTree(Node root) {
22-
if (root == null)
23-
return 0;
24-
25-
return Math.max(heightOfBinaryTree(root.left), heightOfBinaryTree(root.right)) + 1;
26-
}
27-
2830
static Node createTree() {
2931

3032
Node root = null;

src/sde_sheet/DepthOfBinaryTree.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sde_sheet;
2+
3+
import java.util.Scanner;
4+
5+
import tree_implementations.Node;
6+
7+
public class DepthOfBinaryTree {
8+
9+
public static int depthOfBinaryTree(Node root) {
10+
if (root == null)
11+
return 0;
12+
13+
// Here we take the maximum height of left subtree and right subtree
14+
// Then we add one for counting the root for the height
15+
return Math.max(depthOfBinaryTree(root.left), depthOfBinaryTree(root.right)) + 1;
16+
}
17+
18+
static Scanner sc = null;
19+
20+
public static void main(String[] args) {
21+
22+
sc = new Scanner(System.in);
23+
24+
Node root = createTree();
25+
26+
System.out.println("Height of the tree: " + depthOfBinaryTree(root));
27+
28+
}
29+
30+
static Node createTree() {
31+
32+
Node root = null;
33+
System.out.println("Enter data: ");
34+
int data = sc.nextInt();
35+
36+
if (data == -1)
37+
return null;
38+
39+
root = new Node(data);
40+
41+
System.out.println("Enter left for " + data);
42+
root.left = createTree();
43+
44+
System.out.println("Enter right for " + data);
45+
root.right = createTree();
46+
47+
return root;
48+
}
49+
50+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package sde_sheet;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
import java.util.Stack;
7+
8+
import tree_implementations.Node;
9+
10+
/*
11+
* Time Complexity: O(N)
12+
* where N is the no. of nodes in the tree
13+
* Space Complexity: O(N)
14+
* where N is the no. of nodes in the tree
15+
* (don't take the space of answer DS into account)
16+
*/
17+
18+
class Pair {
19+
Node node;
20+
int num;
21+
22+
Pair(Node _node, int _num) {
23+
num = _num;
24+
node = _node;
25+
}
26+
}
27+
28+
class Answer {
29+
List<Integer> pre;
30+
List<Integer> in;
31+
List<Integer> post;
32+
33+
Answer(List<Integer> _pre, List<Integer> _in, List<Integer> _post) {
34+
pre = _pre;
35+
in = _in;
36+
post = _post;
37+
}
38+
}
39+
40+
public class PreInPostOrderTraversals {
41+
42+
public static Answer preInPostOrderTraversals(Node root) {
43+
Stack<Pair> stack = new Stack<Pair>();
44+
stack.push(new Pair(root, 1));
45+
List<Integer> preOrder = new ArrayList<>();
46+
List<Integer> inOrder = new ArrayList<>();
47+
List<Integer> postOrder = new ArrayList<>();
48+
if (root == null)
49+
return null;
50+
51+
while (!stack.isEmpty()) {
52+
Pair it = stack.pop();
53+
54+
// this is part of pre order
55+
// increment 1 or 2
56+
// push the left side of the tree
57+
if (it.num == 1) {
58+
preOrder.add(it.node.data);
59+
it.num++;
60+
stack.push(it);
61+
62+
if (it.node.left != null) {
63+
stack.push(new Pair(it.node.left, 1));
64+
}
65+
}
66+
67+
// this is a part of in order
68+
// increment 2 to 3
69+
// push right
70+
else if (it.num == 2) {
71+
inOrder.add(it.node.data);
72+
it.num++;
73+
stack.push(it);
74+
75+
if (it.node.right != null) {
76+
stack.push(new Pair(it.node.right, 1));
77+
}
78+
79+
}
80+
81+
// don't push it back again
82+
else {
83+
postOrder.add(it.node.data);
84+
}
85+
}
86+
87+
// Only for Eclipse
88+
return new Answer(preOrder, inOrder, postOrder);
89+
90+
}
91+
92+
static Scanner sc = null;
93+
94+
public static void main(String[] args) {
95+
96+
sc = new Scanner(System.in);
97+
98+
Node root = createTree();
99+
100+
Answer ans = preInPostOrderTraversals(root);
101+
System.out.print("Preorder: ");
102+
for (int data : ans.pre) {
103+
System.out.print(data + ", ");
104+
}
105+
System.out.print("Inorder: ");
106+
for (int data : ans.in) {
107+
System.out.print(data + ", ");
108+
}
109+
System.out.print("Postorder: ");
110+
for (int data : ans.post) {
111+
System.out.print(data + ", ");
112+
}
113+
114+
}
115+
116+
static Node createTree() {
117+
118+
Node root = null;
119+
System.out.println("Enter data: ");
120+
int data = sc.nextInt();
121+
122+
if (data == -1)
123+
return null;
124+
125+
root = new Node(data);
126+
127+
System.out.println("Enter left for " + data);
128+
root.left = createTree();
129+
130+
System.out.println("Enter right for " + data);
131+
root.right = createTree();
132+
133+
return root;
134+
}
135+
136+
}
137+
138+
/*
139+
* Example 1:
140+
141+
1
142+
2
143+
3
144+
-1
145+
-1
146+
4
147+
-1
148+
-1
149+
5
150+
6
151+
-1
152+
-1
153+
7
154+
-1
155+
-1
156+
157+
Preorder: 1, 2, 3, 4, 5, 6, 7, Inorder: 3, 2, 4, 1, 6, 5, 7, Postorder: 3, 4, 2, 6, 7, 5, 1,
158+
159+
*/

0 commit comments

Comments
 (0)