File tree 2 files changed +40
-0
lines changed
src/main/java/org/example
2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 1
1
# Binary-Tree-Algorithms
2
+
3
+ * Insert
4
+ * Find
5
+ * Height of a tree
6
+ * Minimum Value In A Tree
7
+ * Equality Between Trees
8
+ * Validating A Binary Search Tree
9
+ * Nodes At K Distance
10
+ * Print Leaves
11
+ * Sum Of Nodes
12
+ * Invert Binary Tree
13
+ * Depth First Traversals
14
+ - PreOrder
15
+ - InOrder
16
+ - PostOrder
17
+ * Breadth First Search / Level Order Traversal
18
+ - Using Height Of Tree And Nodes At K Distance
19
+ - Using Queue
20
+
Original file line number Diff line number Diff line change 1
1
package org .example ;
2
2
3
3
import java .util .ArrayList ;
4
+ import java .util .LinkedList ;
4
5
import java .util .List ;
6
+ import java .util .Queue ;
5
7
6
8
public class Tree {
7
9
private class Node {
@@ -174,6 +176,25 @@ public void levelOrderTraversal() {
174
176
nodesAtKDistance (i ).forEach (System .out ::println );
175
177
}
176
178
}
179
+ public void levelOrderTraversalUsingQueue (){
180
+ if (root == null ) return ;
181
+
182
+ Queue <Node > queue = new LinkedList <>();
183
+ queue .add (root );
184
+
185
+ while (!queue .isEmpty ()){
186
+ var current = queue .poll ();
187
+ System .out .println (current );
188
+
189
+ if (current .leftChild != null ){
190
+ queue .add (current .leftChild );
191
+ }
192
+
193
+ if (current .rightChild != null ) {
194
+ queue .add (current .rightChild );
195
+ }
196
+ }
197
+ }
177
198
public void printLeaves (){
178
199
printLeaves (root );
179
200
}
You can’t perform that action at this time.
0 commit comments