forked from avinashbest/java-coding-ninjas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberOfNodes.java
More file actions
25 lines (20 loc) · 770 Bytes
/
NumberOfNodes.java
File metadata and controls
25 lines (20 loc) · 770 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
package binaryTrees2;
import binaryTrees1.BinaryTreeNode;
import static binaryTrees1.BinaryTreeInput.printBinaryTree;
import static binaryTrees1.BinaryTreeInput.takeTreeInputDetailed;
public class NumberOfNodes {
/*
* Time Complexity: O(n)
* */
public static int noOfNodes(BinaryTreeNode<Integer> root) {
if (root == null) return 0;
return 1 + noOfNodes(root.left) + noOfNodes(root.right);
}
public static void main(String[] args) {
var root = takeTreeInputDetailed(true, 0, true);
System.out.println("----------The Tree is----------");
printBinaryTree(root);
System.out.println("-------------------------------");
System.out.println("Number of Nodes = " + noOfNodes(root));
}
}