-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeLeafNodes.java
More file actions
36 lines (34 loc) · 990 Bytes
/
BinaryTreeLeafNodes.java
File metadata and controls
36 lines (34 loc) · 990 Bytes
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
32
33
34
35
36
package Ds.Achievers;
public class BinaryTreeLeafNodes {
static class Node{
int data;
Node left;
Node right;
Node(int d){
data = d;
left = right = null;
}
}
static Node root;
void printLeafNodes(Node root){
if (root == null)
return;
if (root.left == null && root.right == null)
System.out.print(root.data+" ");
if (root.left != null)
printLeafNodes(root.left);
if (root.right != null)
printLeafNodes(root.right);
}
public static void main(String[] args) {
BinaryTreeLeafNodes tree = new BinaryTreeLeafNodes();
root = new Node(100);
root.left = new Node(99);
root.right = new Node(98);
root.left.left = new Node(97);
root.left.right = new Node(96);
root.right.left = new Node(95);
root.right.right = new Node(94);
tree.printLeafNodes(root);
}
}