FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
java-coding-ninjas/binaryTrees1/PrintNodesAtDepthK.java at master · avinashbest/java-coding-ninjas · GitHub
avinashbest
/
java-coding-ninjas
Public
Notifications
You must be signed in to change notification settings
Fork
11
Star
18
Code
Issues
0
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
java-coding-ninjas
/
binaryTrees1
/
PrintNodesAtDepthK.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
31 lines (26 loc) · 1.02 KB
Breadcrumbs
java-coding-ninjas
/
binaryTrees1
/
PrintNodesAtDepthK.java
Copy path
File metadata and controls
31 lines (26 loc) · 1.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
);
}
}
Back
|
FazBrowse Home
|
New Git URL