forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeafNodes.java
40 lines (36 loc) · 923 Bytes
/
LeafNodes.java
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
37
38
39
40
package com.rampatra.trees;
import com.rampatra.base.BinaryNode;
import com.rampatra.base.BinaryTree;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 6/26/15
* @time: 6:08 PM
*/
public class LeafNodes {
/**
* Returns the number of leaf nodes in a binary tree.
*
* @return
*/
public static <E extends Comparable<E>> int countLeafNodes(BinaryNode<E> node) {
if (node == null) {
return 0;
} else if (node.left == null && node.right == null) {
return 1;
} else {
return countLeafNodes(node.left) + countLeafNodes(node.right);
}
}
public static void main(String[] args) {
BinaryTree<Integer> bt = new BinaryTree<>();
bt.put(6);
bt.put(3);
bt.put(5);
bt.put(7);
bt.put(8);
bt.put(9);
System.out.println(countLeafNodes(bt.root));
}
}