-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPrintNodesAtDepthK.java
More file actions
31 lines (26 loc) · 1.02 KB
/
PrintNodesAtDepthK.java
File metadata and controls
31 lines (26 loc) · 1.02 KB
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
package binaryTrees1;
import java.util.Scanner;
import static binaryTrees1.BinaryTreeInput.printBinaryTree;
import static binaryTrees1.BinaryTreeInput.takeTreeInputDetailed;
public class PrintNodesAtDepthK {
public static void printNodeAtDepthK(BinaryTreeNode<Integer> root, int K) {
if (root == null) return;
if (K == 0) {
System.out.println(root.data);
return;
}
// the distance of depth ignore root is 1 smaller in left subtree and right subtree
printNodeAtDepthK(root.left, K - 1);
printNodeAtDepthK(root.right, K - 1);
}
public static void main(String[] args) {
var root = takeTreeInputDetailed(true, 0, true);
System.out.println("----------The Tree is----------");
printBinaryTree(root);
System.out.println("-------------------------------");
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Depth to print: ");
int K = scan.nextInt();
printNodeAtDepthK(root, K);
}
}