Q:

Java program to determine whether all leaves are at same level

belongs to collection: Java Tree Programs

0

In this program, we need to check whether all the leaves of the given binary tree are at same level or not.

A Node is said to be leaf if it doesn't have any child node. In the below diagram, nodes 4, 5 and 6 are leaf nodes as they don't have any child node. Nodes 4, 5 and 6 are present at the same level, i.e., Level 2.

Algorithm

  • Define Node class which has three attributes namely: data left and right. Here, left represents the left child of the node and right represents the right child of the node.
  • When a node is created, data will pass to data attribute of the node and both left and right will be set to null.
  • Define another class which has two attribute root and level.
    • Root represents the root node of the tree and initializes it to null.
    • The level will be used to store the level of the first encountered leaf node.

a. isSameLevel() will check whether all leaves of given binary tree are at same level or not:

  • It checks whether the root is null, which means the tree is empty.
  • If the tree is not empty, traverse through the tree and check for leaf node whose left and right children are null.
  • CurrentLevel will keep track of current level being traversed.
  • When the first leaf node is encountered, store the value of currentLevel in variable level.
  • Traverse recursively through all level, check for subsequent leaf nodes. If currentLevel of all leaf is equal to the value stored in level then, all leaves are at same level.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

public class LeafLevel {  
  
    //Represent a node of binary tree  
    public static class Node{  
        int data;  
        Node left;  
        Node right;  
  
        public Node(int data){  
            //Assign data to the new node, set left and right children to null  
            this.data = data;  
            this.left = null;  
            this.right = null;  
            }  
        }  
  
    //Represent the root of binary tree  
    public Node root;  
  
    //It will store level of first encountered leaf  
    public static int level = 0;  
  
    public LeafLevel(){  
        root = null;  
    }  
  
    //isSameLevel() will check whether all leaves of the binary tree is at same level or not  
    public boolean isSameLevel(Node temp, int currentLevel ) {  
  
        //Check whether tree is empty  
        if(root == null){  
          System.out.println("Tree is empty");  
          return true;  
        }  
        else {  
            //Checks whether node is null  
            if(temp==null)  
                return true;  
  
            if(temp.left == null && temp.right == null) {  
                //If first leaf is encountered, set level to current level  
                if(level == 0) {  
                    level = currentLevel ;  
                    return true;  
                }  
                //Checks whether the other leaves are at same level of that of first leaf  
                else  
                   return (level == currentLevel) ;  
             }  
  
            //Checks for leaf node in left and right subtree recursively.  
            return  (isSameLevel(temp.left, currentLevel + 1) && isSameLevel(temp.right, currentLevel + 1)) ;  
         }  
    }  
  
    public static void main (String[] args) {  
  
        LeafLevel bt = new LeafLevel();  
        //Add nodes to the binary tree  
        bt.root = new Node(1);  
        bt.root.left = new Node(2);  
        bt.root.right = new Node(3);  
        bt.root.left.left = new Node(4);  
        bt.root.right.left = new Node(5);  
        bt.root.right.right = new Node(6);  
  
        //Checks whether all leaves of given binary tree is at same level  
        if(bt.isSameLevel(bt.root, 1))  
            System.out.println("All leaves are at same level");  
        else  
            System.out.println("All leaves are not at same level");  
  }  
}  

Output:

All leaves are at same level

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java program to determine whether two trees are id... >>
<< Java program to convert Binary Tree to Binary Sear...