Check if removing an edge can divide a Binary Tree in two halves
Last Updated :
30 Jun, 2022
Given a Binary Tree, find if there exists an edge whose removal creates two trees of equal size.
Examples:
Input : root of following tree
5
/ \
1 6
/ / \
3 7 4
Output : true
Removing edge 5-6 creates two trees of equal size
Input : root of following tree
5
/ \
1 6
/ \
7 4
/ \ \
3 2 8
Output : false
There is no edge whose removal creates two trees
of equal size.
Source- Kshitij IIT KGP
Method 1 (Simple): First count number of nodes in whole tree. Let count of all nodes be n. Now traverse tree and for every node, find size of subtree rooted with this node. Let the subtree size be s. If n-s is equal to s, then return true, else false.
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* left, *right;
};
struct Node* newNode( int x)
{
struct Node* temp = new Node;
temp->data = x;
temp->left = temp->right = NULL;
return temp;
};
int count(Node* root)
{
if (root==NULL)
return 0;
return count(root->left) + count(root->right) + 1;
}
bool checkRec(Node* root, int n)
{
if (root ==NULL)
return false ;
if (count(root) == n-count(root))
return true ;
return checkRec(root->left, n) ||
checkRec(root->right, n);
}
bool check(Node *root)
{
int n = count(root);
return checkRec(root, n);
}
int main()
{
struct Node* root = newNode(5);
root->left = newNode(1);
root->right = newNode(6);
root->left->left = newNode(3);
root->right->left = newNode(7);
root->right->right = newNode(4);
check(root)? printf ( "YES" ) : printf ( "NO" );
return 0;
}
|
Java
class Node
{
int key;
Node left, right;
public Node( int key)
{
this .key = key;
left = right = null ;
}
}
class BinaryTree
{
Node root;
int count(Node node)
{
if (node == null )
return 0 ;
return count(node.left) + count(node.right) + 1 ;
}
boolean checkRec(Node node, int n)
{
if (node == null )
return false ;
if (count(node) == n - count(node))
return true ;
return checkRec(node.left, n)
|| checkRec(node.right, n);
}
boolean check(Node node)
{
int n = count(node);
return checkRec(node, n);
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 5 );
tree.root.left = new Node( 1 );
tree.root.right = new Node( 6 );
tree.root.left.left = new Node( 3 );
tree.root.right.left = new Node( 7 );
tree.root.right.right = new Node( 4 );
if (tree.check(tree.root)== true )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
class newNode:
def __init__( self , x):
self .data = x
self .left = self .right = None
def count(root):
if (root = = None ):
return 0
return (count(root.left) +
count(root.right) + 1 )
def checkRec(root, n):
if (root = = None ):
return False
if (count(root) = = n - count(root)):
return True
return (checkRec(root.left, n) or
checkRec(root.right, n))
def check(root):
n = count(root)
return checkRec(root, n)
if __name__ = = '__main__' :
root = newNode( 5 )
root.left = newNode( 1 )
root.right = newNode( 6 )
root.left.left = newNode( 3 )
root.right.left = newNode( 7 )
root.right.right = newNode( 4 )
if check(root):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class Node
{
public int key;
public Node left, right;
public Node( int key)
{
this .key = key;
left = right = null ;
}
}
class GFG
{
public Node root;
public virtual int count(Node node)
{
if (node == null )
{
return 0;
}
return count(node.left) +
count(node.right) + 1;
}
public virtual bool checkRec(Node node, int n)
{
if (node == null )
{
return false ;
}
if (count(node) == n - count(node))
{
return true ;
}
return checkRec(node.left, n) ||
checkRec(node.right, n);
}
public virtual bool check(Node node)
{
int n = count(node);
return checkRec(node, n);
}
public static void Main( string [] args)
{
GFG tree = new GFG();
tree.root = new Node(5);
tree.root.left = new Node(1);
tree.root.right = new Node(6);
tree.root.left.left = new Node(3);
tree.root.right.left = new Node(7);
tree.root.right.right = new Node(4);
if (tree.check(tree.root) == true )
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
class Node
{
constructor(key)
{
this .key=key;
this .left= this .right= null ;
}
}
function count(node)
{
if (node == null )
return 0;
return count(node.left) +
count(node.right) + 1;
}
function checkRec(node,n)
{
if (node == null )
return false ;
if (count(node) == n - count(node))
return true ;
return checkRec(node.left, n)
|| checkRec(node.right, n);
}
function check(node)
{
let n = count(node);
return checkRec(node, n);
}
let root = new Node(5);
root.left = new Node(1);
root.right = new Node(6);
root.left.left = new Node(3);
root.right.left = new Node(7);
root.right.right = new Node(4);
if (check(root)== true )
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time complexity: O(n2) where n is number of nodes in given Binary Tree.
Auxiliary Space: O(n) for call stack since using recursion, where n is no of nodes in binary tree
Method 2 (Efficient): We can find the solution in O(n) time. The idea is to traverse tree in bottom up manner and while traversing keep updating size and keep checking if there is a node that follows the required property.
Below is the implementation of above idea.
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int data;
struct Node* left, *right;
};
struct Node* newNode( int x)
{
struct Node* temp = new Node;
temp->data = x;
temp->left = temp->right = NULL;
return temp;
};
int count(Node* root)
{
if (root==NULL)
return 0;
return count(root->left) + count(root->right) + 1;
}
int checkRec(Node* root, int n, bool &res)
{
if (root == NULL)
return 0;
int c = checkRec(root->left, n, res) + 1 +
checkRec(root->right, n, res);
if (c == n-c)
res = true ;
return c;
}
bool check(Node *root)
{
int n = count(root);
bool res = false ;
checkRec(root, n, res);
return res;
}
int main()
{
struct Node* root = newNode(5);
root->left = newNode(1);
root->right = newNode(6);
root->left->left = newNode(3);
root->right->left = newNode(7);
root->right->right = newNode(4);
check(root)? printf ( "YES" ) : printf ( "NO" );
return 0;
}
|
Java
class Node
{
int key;
Node left, right;
public Node( int key)
{
this .key = key;
left = right = null ;
}
}
class Res
{
boolean res = false ;
}
class BinaryTree
{
Node root;
int count(Node node)
{
if (node == null )
return 0 ;
return count(node.left) + count(node.right) + 1 ;
}
int checkRec(Node root, int n, Res res)
{
if (root == null )
return 0 ;
int c = checkRec(root.left, n, res) + 1
+ checkRec(root.right, n, res);
if (c == n - c)
res.res = true ;
return c;
}
boolean check(Node root)
{
int n = count(root);
Res res = new Res();
checkRec(root, n, res);
return res.res;
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 5 );
tree.root.left = new Node( 1 );
tree.root.right = new Node( 6 );
tree.root.left.left = new Node( 3 );
tree.root.right.left = new Node( 7 );
tree.root.right.right = new Node( 4 );
if (tree.check(tree.root) == true )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
class Node:
def __init__( self , x):
self .key = x
self .left = None
self .right = None
def count(node):
if (node = = None ):
return 0
return (count(node.left) +
count(node.right) + 1 )
def checkRec(root, n):
global res
if (root = = None ):
return 0
c = (checkRec(root.left, n) + 1 +
checkRec(root.right, n))
if (c = = n - c):
res = True
return c
def check(root):
n = count(root)
checkRec(root, n)
if __name__ = = '__main__' :
res = False
root = Node( 5 )
root.left = Node( 1 )
root.right = Node( 6 )
root.left.left = Node( 3 )
root.right.left = Node( 7 )
root.right.right = Node( 4 )
check(root)
if res:
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
public class Node
{
public int key;
public Node left, right;
public Node( int key)
{
this .key = key;
left = right = null ;
}
}
public class Res
{
public bool res = false ;
}
public class BinaryTree
{
public Node root;
public virtual int count(Node node)
{
if (node == null )
{
return 0;
}
return count(node.left) + count(node.right) + 1;
}
public virtual int checkRec(Node root, int n, Res res)
{
if (root == null )
{
return 0;
}
int c = checkRec(root.left, n, res) + 1 + checkRec(root.right, n, res);
if (c == n - c)
{
res.res = true ;
}
return c;
}
public virtual bool check(Node root)
{
int n = count(root);
Res res = new Res();
checkRec(root, n, res);
return res.res;
}
public static void Main( string [] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(5);
tree.root.left = new Node(1);
tree.root.right = new Node(6);
tree.root.left.left = new Node(3);
tree.root.right.left = new Node(7);
tree.root.right.right = new Node(4);
if (tree.check(tree.root) == true )
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
}
}
|
Javascript
<script>
class Node {
constructor(key) {
this .key = key;
this .left = this .right = null ;
}
}
class Res {
constructor(){
this .res = false ;
}
}
var root;
function count( node) {
if (node == null )
return 0;
return count(node.left) + count(node.right) + 1;
}
function checkRec( root , n, res) {
if (root == null )
return 0;
var c = checkRec(root.left, n, res) + 1 + checkRec(root.right, n, res);
if (c == n - c)
res.res = true ;
return c;
}
function check( root) {
var n = count(root);
res = new Res();
checkRec(root, n, res);
return res.res;
}
root = new Node(5);
root.left = new Node(1);
root.right = new Node(6);
root.left.left = new Node(3);
root.right.left = new Node(7);
root.right.right = new Node(4);
if (check(root) == true )
document.write( "YES" );
else
document.write( "NO" );
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Please Login to comment...