Check Full Binary Tree

Java Implementation of checking if the given binary tree is full or not.


class Solution
{
    static class TreeNode{
        int data;
        TreeNode left,right;
        TreeNode(int data)
        {
            this.data=data;
        }
    }
    static boolean isFullTree(TreeNode node)
    {
        if(node==null)
        return true;
        if(node.left==null && node.right==null)
        return true;
        if(node.left==null || node.right==null)
        return false;
        
        return isFullTree(node.left) && isFullTree(node.right);
    }
     public static void main(String[] args)
    {
        TreeNode root=new TreeNode(1);
        root.left=new TreeNode(2);
        root.right=new TreeNode(3);
        root.left.left=new TreeNode(4);
        root.left.right=new TreeNode(7);
        
        boolean check=isFullTree(root);
        System.out.println(check);
         
    }
    }

Output: true

Time Complexity- O(n)

Leave a comment

Your email address will not be published. Required fields are marked *

Pin It on Pinterest