📜  检查二叉树是否为满二叉树 |迭代方法

📅  最后修改于: 2022-05-13 01:57:16.960000             🧑  作者: Mango

检查二叉树是否为满二叉树 |迭代方法

给定一棵包含n 个节点的二叉树。问题是检查给定的二叉树是否是完整的二叉树。完全二叉树定义为所有节点都具有零个或两个子节点的二叉树。相反,满二叉树中没有节点,只有一个子节点。
例子:

Input : 
           1
         /   \
        2     3
       / \  
      4   5
Output : Yes

Input :
           1
         /   \
        2     3
       /  
      4   
Output :No

方法:在上一篇文章中讨论了递归解决方案。在这篇文章中,采用了一种迭代方法。使用队列执行树的迭代级别顺序遍历。对于遇到的每个节点,请按照以下步骤操作:

  1. 如果 (node->left == NULL && node->right == NULL),它是一个叶子节点。丢弃它并开始处理队列中的下一个节点。
  2. 如果 (node->left == NULL || node->right == NULL),则表示节点的唯一子节点存在。返回 false,因为二叉树不是完整的二叉树。
  3. 否则,将节点的左右子节点推入队列。

如果队列中的所有节点都得到处理而不返回 false,则返回 true,因为二叉树是完整的二叉树。

C++
// C++ implementation to check whether a binary
// tree is a full binary tree or not
#include 
using namespace std;
 
// structure of a node of binary tree
struct Node {
    int data;
    Node *left, *right;
};
 
// function to get a new node
Node* getNode(int data)
{
    // allocate space
    Node* newNode = (Node*)malloc(sizeof(Node));
 
    // put in the data
    newNode->data = data;
    newNode->left = newNode->right = NULL;
    return newNode;
}
 
// function to check whether a binary tree
// is a full binary tree or not
bool isFullBinaryTree(Node* root)
{
    // if tree is empty
    if (!root)
        return true;
 
    // queue used for level order traversal
    queue q;
 
    // push 'root' to 'q'
    q.push(root);
 
    // traverse all the nodes of the binary tree
    // level by level until queue is empty
    while (!q.empty()) {
        // get the pointer to 'node' at front
        // of queue
        Node* node = q.front();
        q.pop();
 
        // if it is a leaf node then continue
        if (node->left == NULL && node->right == NULL)
            continue;
 
        // if either of the child is not null and the
        // other one is null, then binary tree is not
        // a full binary tee
        if (node->left == NULL || node->right == NULL)
            return false;
 
        // push left and right childs of 'node'
        // on to the queue 'q'
        q.push(node->left);
        q.push(node->right);
    }
 
    // binary tree is a full binary tee
    return true;
}
 
// Driver program to test above
int main()
{
    Node* root = getNode(1);
    root->left = getNode(2);
    root->right = getNode(3);
    root->left->left = getNode(4);
    root->left->right = getNode(5);
 
    if (isFullBinaryTree(root))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation to check whether a binary
// tree is a full binary tree or not
import java.util.*;
class GfG {
 
// structure of a node of binary tree
static class Node {
    int data;
    Node left, right;
}
 
// function to get a new node
static Node getNode(int data)
{
    // allocate space
    Node newNode = new Node();
 
    // put in the data
    newNode.data = data;
    newNode.left = null;
    newNode.right = null;
    return newNode;
}
 
// function to check whether a binary tree
// is a full binary tree or not
static boolean isFullBinaryTree(Node root)
{
    // if tree is empty
    if (root == null)
        return true;
 
    // queue used for level order traversal
    Queue q = new LinkedList ();
 
    // push 'root' to 'q'
    q.add(root);
 
    // traverse all the nodes of the binary tree
    // level by level until queue is empty
    while (!q.isEmpty()) {
        // get the pointer to 'node' at front
        // of queue
        Node node = q.peek();
        q.remove();
 
        // if it is a leaf node then continue
        if (node.left == null && node.right == null)
            continue;
 
        // if either of the child is not null and the
        // other one is null, then binary tree is not
        // a full binary tee
        if (node.left == null || node.right == null)
            return false;
 
        // push left and right childs of 'node'
        // on to the queue 'q'
        q.add(node.left);
        q.add(node.right);
    }
 
    // binary tree is a full binary tee
    return true;
}
 
// Driver program to test above
public static void main(String[] args)
{
    Node root = getNode(1);
    root.left = getNode(2);
    root.right = getNode(3);
    root.left.left = getNode(4);
    root.left.right = getNode(5);
 
    if (isFullBinaryTree(root))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}


Python3
# Python3 program to find deepest
# left leaf Binary search Tree
 
# Helper function that allocates a 
# new node with the given data and
# None left and right pairs.                                    
class getNode:
 
    # Constructor to create a new node
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# function to check whether a binary 
# tree is a full binary tree or not
def isFullBinaryTree( root) :
 
    # if tree is empty
    if (not root) :
        return True
 
    # queue used for level order
    # traversal
    q = []
 
    # append 'root' to 'q'
    q.append(root)
 
    # traverse all the nodes of the
    # binary tree level by level
    # until queue is empty
    while (not len(q)):
         
        # get the pointer to 'node'
        # at front of queue
        node = q[0]
        q.pop(0)
 
        # if it is a leaf node then continue
        if (node.left == None and
            node.right == None):
            continue
 
        # if either of the child is not None 
        # and the other one is None, then
        # binary tree is not a full binary tee
        if (node.left == None or
            node.right == None):
            return False
 
        # append left and right childs
        # of 'node' on to the queue 'q'
        q.append(node.left)
        q.append(node.right)
     
    # binary tree is a full binary tee
    return True
 
# Driver Code
if __name__ == '__main__':
    root = getNode(1)
    root.left = getNode(2)
    root.right = getNode(3)
    root.left.left = getNode(4)
    root.left.right = getNode(5)
 
    if (isFullBinaryTree(root)) :
        print("Yes" )
    else:
        print("No")
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#
// C# implementation to check whether a binary
// tree is a full binary tree or not
using System;
using System.Collections.Generic;
 
class GfG
{
 
// structure of a node of binary tree
public class Node
{
    public int data;
    public Node left, right;
}
 
// function to get a new node
static Node getNode(int data)
{
    // allocate space
    Node newNode = new Node();
 
    // put in the data
    newNode.data = data;
    newNode.left = null;
    newNode.right = null;
    return newNode;
}
 
// function to check whether a binary tree
// is a full binary tree or not
static bool isFullBinaryTree(Node root)
{
    // if tree is empty
    if (root == null)
        return true;
 
    // queue used for level order traversal
    Queue q = new Queue ();
 
    // push 'root' to 'q'
    q.Enqueue(root);
 
    // traverse all the nodes of the binary tree
    // level by level until queue is empty
    while (q.Count!=0) {
        // get the pointer to 'node' at front
        // of queue
        Node node = q.Peek();
        q.Dequeue();
 
        // if it is a leaf node then continue
        if (node.left == null && node.right == null)
            continue;
 
        // if either of the child is not null and the
        // other one is null, then binary tree is not
        // a full binary tee
        if (node.left == null || node.right == null)
            return false;
 
        // push left and right childs of 'node'
        // on to the queue 'q'
        q.Enqueue(node.left);
        q.Enqueue(node.right);
    }
 
    // binary tree is a full binary tee
    return true;
}
 
// Driver code
public static void Main(String[] args)
{
    Node root = getNode(1);
    root.left = getNode(2);
    root.right = getNode(3);
    root.left.left = getNode(4);
    root.left.right = getNode(5);
 
    if (isFullBinaryTree(root))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:

Yes

时间复杂度: O(n)。
辅助空间: O(max),其中max是特定级别的最大节点数。