Check whether a binary tree is a full binary tree or not

Posted codingyangmao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Check whether a binary tree is a full binary tree or not相关的知识,希望对你有一定的参考价值。

Description:

A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has one child node. 

For Example :
技术图片

 

To check whether a binary tree is a full binary tree we need to test the following cases:-

1) If a binary tree node is NULL then it is a full binary tree.
2) If a binary tree node does have empty left and right sub-trees, then it is a full binary tree by definition.
3) If a binary tree node has left and right sub-trees, then it is a part of a full binary tree by definition. In this case recursively check if the left and right sub-trees are also binary trees themselves.
4) In all other combinations of right and left sub-trees, the binary tree is not a full binary tree.

Following is the implementation for checking if a binary tree is a full binary tree.

class Node  
 
    int data; 
    Node left, right; 
   
    Node(int item)  
     
        data = item; 
        left = right = null; 
     
 
   
class BinaryTree  
 
    Node root; 
       
    /* this function checks if a binary tree is full or not */
    boolean isFullTree(Node node) 
     
        // if empty tree 
        if(node == null) 
        return true; 
           
        // if leaf node 
        if(node.left == null && node.right == null ) 
            return true; 
           
        // if both left and right subtrees are not null 
        // the are full 
        if((node.left!=null) && (node.right!=null)) 
            return (isFullTree(node.left) && isFullTree(node.right)); 
           
        // if none work 
        return false; 
     
   
       
    // Driver program 
    public static void main(String args[])  
     
        BinaryTree tree = new BinaryTree(); 
        tree.root = new Node(10); 
        tree.root.left = new Node(20); 
        tree.root.right = new Node(30); 
        tree.root.left.right = new Node(40); 
        tree.root.left.left = new Node(50); 
        tree.root.right.left = new Node(60); 
        tree.root.left.left.left = new Node(80); 
        tree.root.right.right = new Node(70); 
        tree.root.left.left.right = new Node(90); 
        tree.root.left.right.left = new Node(80); 
        tree.root.left.right.right = new Node(90); 
        tree.root.right.left.left = new Node(80); 
        tree.root.right.left.right = new Node(90); 
        tree.root.right.right.left = new Node(80); 
        tree.root.right.right.right = new Node(90); 
           
        if(tree.isFullTree(tree.root)) 
            System.out.print("The binary tree is full"); 
        else
            System.out.print("The binary tree is not full");  
     
 
   
// This code is contributed by Mayank Jaiswal  

 

以上是关于Check whether a binary tree is a full binary tree or not的主要内容,如果未能解决你的问题,请参考以下文章

为啥改不了hosts文件 please check whether if this file is opened in another

[LeetCode] 2068. Check Whether Two Strings are Almost Equivalent

LeetCode 958. Check Completeness of a Binary Tree

check whether the subset(no need to be consective) and be sum of X

vt-is-UTF8 - check whether current VT is in UTF8- or byte-mode. 检查当前VT是否处于VTF8模式或是字节模式.

Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST