📌  相关文章
📜  检查给定的二叉树是否有一个 1 和 0 相等的子树

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

检查给定的二叉树是否有一个 1 和 0 相等的子树

给定一棵二叉树,其节点处的数据为 0 或 1。任务是找出是否存在具有相同数量的 1 和 0 的子树。

例子:

方法:想法是将树的数据0更改为-1。这样就很容易找到具有相同数量的 0 和 1 的子树。将所有 0 转换为 -1 后,创建一个求和树。创建总和树后,每个节点将包含位于其下的所有节点的总和。

再次遍历树,如果有一个和为0的节点,则表示存在一个1和-1个数相等的子树,即1和0的个数相等。

下面是上述方法的实现:

C++
// C++ program to check if there exist a
// subtree with equal number of 1's and 0's
 
#include 
using namespace std;
 
// Binary Tree Node
struct node {
    int data;
    struct node *right, *left;
};
 
// Utility function to create a new node
struct node* newnode(int key)
{
    struct node* temp = new node;
    temp->data = key;
    temp->right = NULL;
    temp->left = NULL;
 
    return temp;
}
 
// Function to convert all 0's in the
// tree to -1
void convert(struct node* root)
{
    if (root == NULL) {
        return;
    }
 
    // Move to right subtree
    convert(root->right);
 
    // Replace the 0's with -1 in the tree
    if (root->data == 0) {
        root->data = -1;
    }
 
    // Move to left subtree
    convert(root->left);
}
 
// Function to convert the tree to a SUM tree
int sum_tree(struct node* root)
{
    int a = 0, b = 0;
 
    if (root == NULL) {
        return 0;
    }
 
    a = sum_tree(root->left);
    b = sum_tree(root->right);
 
    root->data = root->data + a + b;
 
    return root->data;
}
 
// Function to check if there exists a subtree
// with equal no of 1s and 0s
int checkSubtree(struct node* root, int d)
{
    if (root == NULL) {
        return 0;
    }
 
    // Check if there is a subtree with equal
    // 1s and 0s or not
    if (d == 0) {
        d = checkSubtree(root->left, d);
    }
 
    if (root->data == 0) {
        d = 1;
        return d;
    }
 
    if (d == 0) {
        d = checkSubtree(root->right, d);
    }
 
    return d;
}
 
// Driver Code
int main()
{
    // Create the Binary Tree
    struct node* root = newnode(1);
    root->right = newnode(0);
    root->right->right = newnode(1);
    root->right->right->right = newnode(1);
    root->left = newnode(0);
    root->left->left = newnode(1);
    root->left->left->left = newnode(1);
    root->left->right = newnode(0);
    root->left->right->left = newnode(1);
    root->left->right->left->left = newnode(1);
    root->left->right->right = newnode(0);
    root->left->right->right->left = newnode(0);
    root->left->right->right->left->left = newnode(1);
 
    // Convert all 0s in tree to -1
    convert(root);
 
    // Convert the tree into a SUM tree
    sum_tree(root);
 
    // Check if required Subtree exists
    int d = 0;
    if (checkSubtree(root, d)) {
        cout << "True" << endl;
    }
    else {
        cout << "False" << endl;
    }
 
    return 0;
}


Java
// Java program to check if there exist a
// subtree with equal number of 1's and 0's
 
import java.util.*;
class GFG{
 
    // Binary Tree Node
    static class node {
        int data;
        node right, left;
    };
     
    // Utility function to create a new node
    static node newnode(int key)
    {
        node temp = new node();
        temp.data = key;
        temp.right = null;
        temp.left = null;
     
        return temp;
    }
     
    // Function to convert all 0's in the
    // tree to -1
    static void convert(node root)
    {
        if (root == null) {
            return;
        }
     
        // Move to right subtree
        convert(root.right);
     
        // Replace the 0's with -1 in the tree
        if (root.data == 0) {
            root.data = -1;
        }
     
        // Move to left subtree
        convert(root.left);
    }
     
    // Function to convert the tree to a SUM tree
    static int sum_tree(node root)
    {
        int a = 0, b = 0;
     
        if (root == null) {
            return 0;
        }
     
        a = sum_tree(root.left);
        b = sum_tree(root.right);
     
        root.data = root.data + a + b;
     
        return root.data;
    }
     
    // Function to check if there exists a subtree
    // with equal no of 1s and 0s
    static int checkSubtree(node root, int d)
    {
        if (root == null) {
            return 0;
        }
     
        // Check if there is a subtree with equal
        // 1s and 0s or not
        if (d == 0) {
            d = checkSubtree(root.left, d);
        }
     
        if (root.data == 0) {
            d = 1;
            return d;
        }
     
        if (d == 0) {
            d = checkSubtree(root.right, d);
        }
     
        return d;
    }
     
    // Driver Code
    public static void main(String args[])
    {
        // Create the Binary Tree
        node root = newnode(1);
        root.right = newnode(0);
        root.right.right = newnode(1);
        root.right.right.right = newnode(1);
        root.left = newnode(0);
        root.left.left = newnode(1);
        root.left.left.left = newnode(1);
        root.left.right = newnode(0);
        root.left.right.left = newnode(1);
        root.left.right.left.left = newnode(1);
        root.left.right.right = newnode(0);
        root.left.right.right.left = newnode(0);
        root.left.right.right.left.left = newnode(1);
     
        // Convert all 0s in tree to -1
        convert(root);
     
        // Convert the tree into a SUM tree
        sum_tree(root);
     
        // Check if required Subtree exists
        int d = 0;
        if (checkSubtree(root, d)>=1) {
            System.out.println("True");
        }
        else {
            System.out.println("False");
        }
    }
}
 
 
// This code is contributed by AbhiThakur


Python3
# Python3 program to check if there exist a
# subtree with equal number of 1's and 0's
 
# Binary Tree Node
class node:
     
    def __init__(self, key):
         
        self.data = key
        self.left = None
        self.right = None
 
# Function to convert all 0's in the
# tree to -1
def convert(root):
 
    if (root == None):
        return
 
    # Move to right subtree
    convert(root.right)
 
    # Replace the 0's with -1
    # in the tree
    if (root.data == 0):
        root.data = -1
 
    # Move to left subtree
    convert(root.left)
 
# Function to convert the tree
# to a SUM tree
def sum_tree(root):
     
    a = 0
    b = 0
 
    if (root == None):
        return 0
 
    a = sum_tree(root.left)
    b = sum_tree(root.right)
 
    root.data = root.data + a + b
 
    return root.data
 
# Function to check if there exists
# a subtree with equal no of 1s and 0s
def checkSubtree(root, d):
     
    if (root == None):
        return 0
 
    # Check if there is a subtree with
    # equal 1s and 0s or not
    if (d == 0):
        d = checkSubtree(root.left, d)
 
    if (root.data == 0):
        d = 1
        return d
 
    if (d == 0):
        d = checkSubtree(root.right, d)
 
    return d
 
# Driver Code
if __name__ == '__main__':
     
    # Create the Binary Tree
    root = node(1)
    root.right = node(0)
    root.right.right = node(1)
    root.right.right.right = node(1)
    root.left = node(0)
    root.left.left = node(1)
    root.left.left.left = node(1)
    root.left.right = node(0)
    root.left.right.left = node(1)
    root.left.right.left.left = node(1)
    root.left.right.right = node(0)
    root.left.right.right.left = node(0)
    root.left.right.right.left.left = node(1)
 
    # Convert all 0s in tree to -1
    convert(root)
 
    # Convert the tree into a SUM tree
    sum_tree(root)
 
    # Check if required Subtree exists
    d = 0
     
    if (checkSubtree(root, d)):
        print("True")
    else:
        print("False")
 
# This code is contributed by mohit kumar 29


C#
// C# program to check if there exist a
// subtree with equal number of 1's and 0's
using System;
 
class GFG{
  
    // Binary Tree Node
    class node {
        public int data;
        public node right, left;
    };
      
    // Utility function to create a new node
    static node newnode(int key)
    {
        node temp = new node();
        temp.data = key;
        temp.right = null;
        temp.left = null;
      
        return temp;
    }
      
    // Function to convert all 0's in the
    // tree to -1
    static void convert(node root)
    {
        if (root == null) {
            return;
        }
      
        // Move to right subtree
        convert(root.right);
      
        // Replace the 0's with -1 in the tree
        if (root.data == 0) {
            root.data = -1;
        }
      
        // Move to left subtree
        convert(root.left);
    }
      
    // Function to convert the tree to a SUM tree
    static int sum_tree(node root)
    {
        int a = 0, b = 0;
      
        if (root == null) {
            return 0;
        }
      
        a = sum_tree(root.left);
        b = sum_tree(root.right);
      
        root.data = root.data + a + b;
      
        return root.data;
    }
      
    // Function to check if there exists a subtree
    // with equal no of 1s and 0s
    static int checkSubtree(node root, int d)
    {
        if (root == null) {
            return 0;
        }
      
        // Check if there is a subtree with equal
        // 1s and 0s or not
        if (d == 0) {
            d = checkSubtree(root.left, d);
        }
      
        if (root.data == 0) {
            d = 1;
            return d;
        }
      
        if (d == 0) {
            d = checkSubtree(root.right, d);
        }
      
        return d;
    }
      
    // Driver Code
    public static void Main(String []args)
    {
        // Create the Binary Tree
        node root = newnode(1);
        root.right = newnode(0);
        root.right.right = newnode(1);
        root.right.right.right = newnode(1);
        root.left = newnode(0);
        root.left.left = newnode(1);
        root.left.left.left = newnode(1);
        root.left.right = newnode(0);
        root.left.right.left = newnode(1);
        root.left.right.left.left = newnode(1);
        root.left.right.right = newnode(0);
        root.left.right.right.left = newnode(0);
        root.left.right.right.left.left = newnode(1);
      
        // Convert all 0s in tree to -1
        convert(root);
      
        // Convert the tree into a SUM tree
        sum_tree(root);
      
        // Check if required Subtree exists
        int d = 0;
        if (checkSubtree(root, d) >= 1) {
            Console.WriteLine("True");
        }
        else {
            Console.WriteLine("False");
        }
    }
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
True

时间复杂度:O(N)
空间复杂度:O(1)