给定一棵树,其每个节点的值为0或1 ,任务是查找给定的二叉树是否包含具有相等数目的0和1的子树,如果找到了该子树,则打印Yes,否则打印No 。
例子:
Input:
Output: Yes
There are two sub-trees with equal number of 1’s and 0’s.
Hence the output is “Yes”
Input:
Output: No
方法:
- 更新树的所有节点,以便它们表示以当前节点为根的子树中所有节点的总和。
- 现在,如果存在某个节点,其值是以该节点为根的树中节点数的一半,则它是有效的子树。
- 如果不存在这样的节点,则打印No。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// To store whether the tree contains a sub-tree
// with equal number of 0's and 1's
bool hasValidSubTree = false;
// Represents a node of the tree
struct node {
int data;
struct node *right, *left;
};
// 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 perform inorder traversal on
// the tree and print the nodes in that order
void inorder(struct node* root)
{
if (root == NULL)
return;
inorder(root->left);
cout << root->data << endl;
inorder(root->right);
}
// Function to return the size of the
// sub-tree rooted at the current node
int size(struct node* root)
{
int a = 0, b = 0;
// If root is null or the valid sub-tree
// has already been found
if (root == NULL || hasValidSubTree)
return 0;
// Size of the right sub-tree
a = size(root->right);
// 1 is added for the parent
a = a + 1;
// Size of the left sub-tree
b = size(root->left);
// Total size of the tree
// rooted at the current node
a = b + a;
// If the current tree has equal
// number of 0's and 1's
if (a % 2 == 0 && root->data == a / 2)
hasValidSubTree = true;
return a;
}
// Function to update and return the sum
// of all the tree nodes rooted at
// the passed node
int sum_tree(struct node* root)
{
if (root == NULL)
return 0;
int a = 0, b = 0;
// If left child exists
if (root->left != NULL)
a = sum_tree(root->left);
// If right child exists
if (root->right != NULL)
b = sum_tree(root->right);
root->data += (a + b);
return root->data;
}
// Driver code
int main()
{
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(1);
root->left->right->right->left->left = newnode(1);
sum_tree(root);
size(root);
if (hasValidSubTree)
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation of the approach
import java.util.Comparator;
class GFG
{
// To store whether the tree contains a sub-tree
// with equal number of 0's and 1's
static boolean hasValidSubTree = false;
// Represents a node of the tree
static class node
{
int data;
node right, left;
};
// 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 perform inorder traversal on
// the tree and print the nodes in that order
static void inorder( node root)
{
if (root == null)
return;
inorder(root.left);
System.out.print(root.data);
inorder(root.right);
}
// Function to return the size of the
// sub-tree rooted at the current node
static int size( node root)
{
int a = 0, b = 0;
// If root is null or the valid sub-tree
// has already been found
if (root == null || hasValidSubTree)
return 0;
// Size of the right sub-tree
a = size(root.right);
// 1 is added for the parent
a = a + 1;
// Size of the left sub-tree
b = size(root.left);
// Total size of the tree
// rooted at the current node
a = b + a;
// If the current tree has equal
// number of 0's and 1's
if (a % 2 == 0 && root.data == a / 2)
hasValidSubTree = true;
return a;
}
// Function to update and return the sum
// of all the tree nodes rooted at
// the passed node
static int sum_tree( node root)
{
if (root == null)
return 0;
int a = 0, b = 0;
// If left child exists
if (root.left != null)
a = sum_tree(root.left);
// If right child exists
if (root.right != null)
b = sum_tree(root.right);
root.data += (a + b);
return root.data;
}
// Driver code
public static void main(String args[])
{
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(1);
root.left.right.right.left.left = newnode(1);
sum_tree(root);
size(root);
if (hasValidSubTree)
System.out.print( "Yes");
else
System.out.print( "No");
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the approach
class node:
def __init__(self, key):
self.data = key
self.left = None
self.right = None
# Function to perform inorder traversal on
# the tree and print the nodes in that order
def inorder(root):
if root == None:
return
inorder(root.left)
print(root.data)
inorder(root.right)
# Function to return the size of the
# sub-tree rooted at the current node
def size(root):
a, b = 0, 0
global hasValidSubTree
# If root is null or the valid
# sub-tree has already been found
if root == None or hasValidSubTree:
return 0
# Size of the right sub-tree
a = size(root.right)
# 1 is added for the parent
a = a + 1
# Size of the left sub-tree
b = size(root.left)
# Total size of the tree
# rooted at the current node
a = b + a
# If the current tree has equal
# number of 0's and 1's
if a % 2 == 0 and root.data == a // 2:
hasValidSubTree = True
return a
# Function to update and return the sum
# of all the tree nodes rooted at
# the passed node
def sum_tree(root):
if root == None:
return 0
a, b = 0, 0
# If left child exists
if root.left != None:
a = sum_tree(root.left)
# If right child exists
if root.right != None:
b = sum_tree(root.right)
root.data += (a + b)
return root.data
# Driver code
if __name__ == "__main__":
# To store whether the tree contains a
# sub-tree with equal number of 0's and 1's
hasValidSubTree = False
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(1)
root.left.right.right.left.left = node(1)
sum_tree(root)
size(root)
if hasValidSubTree:
print("Yes")
else:
print("No")
# This code is contributed by Rituraj Jain
C#
// C# implementation of the approach
using System;
class GFG
{
// To store whether the tree contains a sub-tree
// with equal number of 0's and 1's
static bool hasValidSubTree = false;
// Represents a node of the tree
public class node
{
public int data;
public node right, left;
};
// 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 perform inorder traversal on
// the tree and print the nodes in that order
static void inorder( node root)
{
if (root == null)
return;
inorder(root.left);
Console.Write(root.data);
inorder(root.right);
}
// Function to return the size of the
// sub-tree rooted at the current node
static int size( node root)
{
int a = 0, b = 0;
// If root is null or the valid sub-tree
// has already been found
if (root == null || hasValidSubTree)
return 0;
// Size of the right sub-tree
a = size(root.right);
// 1 is added for the parent
a = a + 1;
// Size of the left sub-tree
b = size(root.left);
// Total size of the tree
// rooted at the current node
a = b + a;
// If the current tree has equal
// number of 0's and 1's
if (a % 2 == 0 && root.data == a / 2)
hasValidSubTree = true;
return a;
}
// Function to update and return the sum
// of all the tree nodes rooted at
// the passed node
static int sum_tree( node root)
{
if (root == null)
return 0;
int a = 0, b = 0;
// If left child exists
if (root.left != null)
a = sum_tree(root.left);
// If right child exists
if (root.right != null)
b = sum_tree(root.right);
root.data += (a + b);
return root.data;
}
// Driver code
public static void Main(String []args)
{
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(1);
root.left.right.right.left.left = newnode(1);
sum_tree(root);
size(root);
if (hasValidSubTree)
Console.Write( "Yes");
else
Console.Write( "No");
}
}
// This code contributed by Rajput-Ji
输出:
No