public class CalculateSizeUsingRecusrion {
public static void main(String args[]) {
Node rootNode = new Node("4");
rootNode.left = new Node("2");
rootNode.left.left = new Node("1");
rootNode.left.right = new Node("3");
rootNode.right = new Node("6");
rootNode.right.right = new Node("7");
rootNode.right.left = new Node("5");
System.out.println(calcSize(rootNode));
}
static int calcSize(Node node) {
if(node==null) {
return 0;
}
return calcSize(node.left)+1+calcSize(node.right);
}
}
class BinayTree{
static class Node{
String value;
Node left;
Node right;
Node(String val){
value = val;
left = right = null;
}
}
}
output: 7
public static void main(String args[]) {
Node rootNode = new Node("4");
rootNode.left = new Node("2");
rootNode.left.left = new Node("1");
rootNode.left.right = new Node("3");
rootNode.right = new Node("6");
rootNode.right.right = new Node("7");
rootNode.right.left = new Node("5");
System.out.println(calcSize(rootNode));
}
static int calcSize(Node node) {
if(node==null) {
return 0;
}
return calcSize(node.left)+1+calcSize(node.right);
}
}
class BinayTree{
static class Node{
String value;
Node left;
Node right;
Node(String val){
value = val;
left = right = null;
}
}
}
output: 7
No comments:
Post a Comment