File tree Expand file tree Collapse file tree 2 files changed +40
-0
lines changed
src/main/java/org/example Expand file tree Collapse file tree 2 files changed +40
-0
lines changed Original file line number Diff line number Diff line change 11# 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 11package org .example ;
22
33import java .util .ArrayList ;
4+ import java .util .LinkedList ;
45import java .util .List ;
6+ import java .util .Queue ;
57
68public class Tree {
79 private class Node {
@@ -174,6 +176,25 @@ public void levelOrderTraversal() {
174176 nodesAtKDistance (i ).forEach (System .out ::println );
175177 }
176178 }
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+ }
177198 public void printLeaves (){
178199 printLeaves (root );
179200 }
You can’t perform that action at this time.
0 commit comments