Write a progtam to calculate size of a tree
Recursion Steps:
Size of a tree= 1+size of left subtree + size of right subtree
Base case : return zero if a node is null
Code: Time:O(n)
class Node{
int data;
Node left, right;
Node(int data){
this.data = data;
}
}
public class SizeOfBinaryTree {
static int calculateSize(Node node){
if(node == null){
return 0;
}
return (1+calculateSize(node.left)+calculateSize(node.right));
}
public static void main(String[] args){
Node node = new Node(5);
node.left = new Node(6);
node.right = new Node(7);
node.right.right = new Node(8);
System.out.println(calculateSize(node));
}
}
Output:
4
Size of a tree= 1+size of left subtree + size of right subtree
Base case : return zero if a node is null
Code: Time:O(n)
class Node{
int data;
Node left, right;
Node(int data){
this.data = data;
}
}
public class SizeOfBinaryTree {
static int calculateSize(Node node){
if(node == null){
return 0;
}
return (1+calculateSize(node.left)+calculateSize(node.right));
}
public static void main(String[] args){
Node node = new Node(5);
node.left = new Node(6);
node.right = new Node(7);
node.right.right = new Node(8);
System.out.println(calculateSize(node));
}
}
Output:
4
Comments
Post a Comment