[Naive Approach] Recursive Method - O(n^2) Time and O(h) Space
The idea is to count the number of nodes in the tree. Let count of all nodes be n. Traverse the tree again and for each node, find size of subtree rooted with this node. Let cnt be the size of subtree size. If n-cnt is equal to cnt, then return true. Otherwise recursively check for left and right subtree.
Below is the implementation of the above approach:
C++
// C++ program to check if there exist an edge whose// removal creates two trees of same size#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// To calculate size of tree with given rootintcountNodes(Node*root){if(root==nullptr)return0;returncountNodes(root->left)+countNodes(root->right)+1;}// This function returns true if there is an edge// whose removal can divide the tree in two halves// n is size of treeboolcheckRec(Node*root,intn){// Base caseif(root==nullptr)returnfalse;// find the size of current subtreeintcnt=countNodes(root);// If unlinking the current subtree from// main tree creates two trees of same size,// return true.if(cnt==n-cnt)returntrue;// Check for rest of the nodesreturncheckRec(root->left,n)||checkRec(root->right,n);}// This function mainly uses checkRec()boolcheck(Node*root){// Count total nodes in given treeintn=countNodes(root);// Now recursively check all nodesreturncheckRec(root,n);}intmain(){// Binary tree // 5// / \ // 1 6// / / \ // 3 7 4Node*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)){cout<<"True"<<endl;}else{cout<<"False"<<endl;}return0;}
C
// C program to check if there exist an edge whose// removal creates two trees of same size#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// To calculate size of tree with given rootintcountNodes(structNode*root){if(root==NULL)return0;returncountNodes(root->left)+countNodes(root->right)+1;}// This function returns true if there is an edge// whose removal can divide the tree in two halves// n is size of treeintcheckRec(structNode*root,intn){// Base caseif(root==NULL)return0;// find the size of current subtreeintcnt=countNodes(root);// If unlinking the current subtree from// main tree creates two trees of same size,// return true.if(cnt==n-cnt)return1;// Check for rest of the nodesreturncheckRec(root->left,n)||checkRec(root->right,n);}// This function mainly uses checkRec()intcheck(structNode*root){// Count total nodes in given treeintn=countNodes(root);// Now recursively check all nodesreturncheckRec(root,n);}structNode*createNode(intx){structNode*newNode=(structNode*)malloc(sizeof(structNode));newNode->data=x;newNode->left=NULL;newNode->right=NULL;returnnewNode;}intmain(){// Binary tree // 5// / \ // 1 6// / / \ // 3 7 4structNode*root=createNode(5);root->left=createNode(1);root->right=createNode(6);root->left->left=createNode(3);root->right->left=createNode(7);root->right->right=createNode(4);if(check(root)){printf("True\n");}else{printf("False\n");}return0;}
Java
// Java program to check if there exist an edge whose// removal creates two trees of same sizeimportjava.util.ArrayList;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGfG{// To calculate size of tree with given rootstaticintcountNodes(Noderoot){if(root==null)return0;returncountNodes(root.left)+countNodes(root.right)+1;}// This function returns true if there is an edge// whose removal can divide the tree in two halvesstaticbooleancheckRec(Noderoot,intn){// Base caseif(root==null)returnfalse;// find the size of current subtreeintcnt=countNodes(root);// If unlinking the current subtree from// main tree creates two trees of same size,// return true.if(cnt==n-cnt)returntrue;// Check for rest of the nodesreturncheckRec(root.left,n)||checkRec(root.right,n);}// This function mainly uses checkRec()staticbooleancheck(Noderoot){// Count total nodes in given treeintn=countNodes(root);// Now recursively check all nodesreturncheckRec(root,n);}publicstaticvoidmain(String[]args){// Binary tree // 5// / \// 1 6// / / \// 3 7 4Noderoot=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)){System.out.println("True");}else{System.out.println("False");}}}
Python
# Python program to check if there exist an edge whose# removal creates two trees of same sizeclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# To calculate size of tree with given rootdefcountNodes(root):ifrootisNone:return0returncountNodes(root.left) \
+countNodes(root.right)+1# This function returns true if there is an edge# whose removal can divide the tree in two halvesdefcheckRec(root,n):# Base caseifrootisNone:returnFalse# find the size of current subtreecnt=countNodes(root)# If unlinking the current subtree from# main tree creates two trees of same size,# return true.ifcnt==n-cnt:returnTrue# Check for rest of the nodesreturncheckRec(root.left,n)orcheckRec(root.right,n)# This function mainly uses checkRec()defcheck(root):# Count total nodes in given treen=countNodes(root)# Now recursively check all nodesreturncheckRec(root,n)if__name__=="__main__":# Binary tree # 5# / \# 1 6# / / \# 3 7 4root=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)ifcheck(root):print("True")else:print("False")
C#
// C# program to check if there exist an edge whose// removal creates two trees of same sizeusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGfG{// To calculate size of tree with given rootstaticintcountNodes(Noderoot){if(root==null)return0;returncountNodes(root.left)+countNodes(root.right)+1;}// This function returns true if there is an edge// whose removal can divide the tree in two halvesstaticboolcheckRec(Noderoot,intn){// Base caseif(root==null)returnfalse;// find the size of current subtreeintcnt=countNodes(root);// If unlinking the current subtree from// main tree creates two trees of same size,// return true.if(cnt==n-cnt)returntrue;// Check for rest of the nodesreturncheckRec(root.left,n)||checkRec(root.right,n);}// This function mainly uses checkRec()staticboolcheck(Noderoot){// Count total nodes in given treeintn=countNodes(root);// Now recursively check all nodesreturncheckRec(root,n);}staticvoidMain(){// Binary tree // 5// / \// 1 6// / / \// 3 7 4Noderoot=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)){Console.WriteLine("True");}else{Console.WriteLine("False");}}}
JavaScript
// JavaScript program to check if there exist an edge whose// removal creates two trees of same sizeclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// To calculate size of tree with given rootfunctioncountNodes(root){if(root===null)return0;returncountNodes(root.left)+countNodes(root.right)+1;}// This function returns true if there is an edge// whose removal can divide the tree in two halvesfunctioncheckRec(root,n){// Base caseif(root===null)returnfalse;// find the size of current subtreeconstcnt=countNodes(root);// If unlinking the current subtree from// main tree creates two trees of same size,// return true.if(cnt===n-cnt)returntrue;// Check for rest of the nodesreturncheckRec(root.left,n)||checkRec(root.right,n);}// This function mainly uses checkRec()functioncheck(root){// Count total nodes in given treeconstn=countNodes(root);// Now recursively check all nodesreturncheckRec(root,n);}// Binary tree // 5// / \// 1 6// / / \// 3 7 4constroot=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)){console.log("True");}else{console.log("False");}
Output
True
[Expected Approach] Bottom-Up Manner - O(n) Time and O(h) Space
The idea is to traverse tree in bottom up manner and while traversing, for each node, find the size of the subtree and check if the current node follows the required property.
Below is the implementation of above approach:
C++
// C++ program to check if there exist an edge whose// removal creates two trees of same size#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// To calculate size of tree with given rootintcountNodes(Node*root){if(root==nullptr)return0;returncountNodes(root->left)+countNodes(root->right)+1;}// This function returns size of tree rooted with given// root. It also set "res" as true if there is an edge// whose removal divides tree in two halves.// n is size of treeintcheckRec(Node*root,intn,bool&ans){// Base caseif(root==nullptr)returnfalse;// find the size of current subtreeintcnt=checkRec(root->left,n,ans)+checkRec(root->right,n,ans)+1;// If unlinking the current subtree from// main tree creates two trees of same size,// return true.if(cnt==n-cnt)ans=true;// return the size of current// subtree.returncnt;}// This function mainly uses checkRec()boolcheck(Node*root){// Count total nodes in given treeintn=countNodes(root);boolans=false;// Now recursively check all nodescheckRec(root,n,ans);returnans;}intmain(){// Binary tree // 5// / \ // 1 6// / / \ // 3 7 4Node*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)){cout<<"True"<<endl;}else{cout<<"False"<<endl;}return0;}
Java
// Java program to check if there exist an edge whose// removal creates two trees of same sizeimportjava.util.*;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=null;right=null;}}classGfG{// To calculate size of tree with given rootstaticintcountNodes(Noderoot){if(root==null)return0;returncountNodes(root.left)+countNodes(root.right)+1;}// This function returns size of tree rooted with given// root. It also sets "ans" as true if there is an edge// whose removal divides tree in two halves.// n is size of treestaticintcheckRec(Noderoot,intn,boolean[]ans){// Base caseif(root==null)return0;// find the size of current subtreeintcnt=checkRec(root.left,n,ans)+checkRec(root.right,n,ans)+1;// If unlinking the current subtree from// main tree creates two trees of same size,// set ans to trueif(cnt==n-cnt)ans[0]=true;// return the size of current subtreereturncnt;}// This function mainly uses checkRec()staticbooleancheck(Noderoot){// Count total nodes in given treeintn=countNodes(root);boolean[]ans={false};// Now recursively check all nodescheckRec(root,n,ans);returnans[0];}publicstaticvoidmain(String[]args){// Binary tree // 5// / \// 1 6// / / \// 3 7 4Noderoot=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)){System.out.println("True");}else{System.out.println("False");}}}
Python
# Python program to check if there exist an edge whose# removal creates two trees of same sizeclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=None# To calculate size of tree with# given rootdefcountNodes(root):ifrootisNone:return0returncountNodes(root.left)+countNodes(root.right)+1# This function returns size of tree rooted with given# root. It also sets "ans" as True if there is an edge# whose removal divides tree in two halves.# n is size of treedefcheckRec(root,n,ans):# Base caseifrootisNone:return0# find the size of current subtreecnt=checkRec(root.left,n,ans)+ \
checkRec(root.right,n,ans)+1# If unlinking the current subtree from# main tree creates two trees of same size,# set ans to Trueifcnt==n-cnt:ans[0]=True# return the size of current subtreereturncnt# This function mainly uses checkRec()defcheck(root):# Count total nodes in given treen=countNodes(root)ans=[False]# Now recursively check all nodescheckRec(root,n,ans)returnans[0]if__name__=="__main__":# Binary tree # 5# / \# 1 6# / / \# 3 7 4root=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)ifcheck(root):print("True")else:print("False")
C#
// C# program to check if there exist an edge whose// removal creates two trees of same sizeusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=null;right=null;}}classGfG{// To calculate size of tree with given rootstaticintcountNodes(Noderoot){if(root==null)return0;returncountNodes(root.left)+countNodes(root.right)+1;}// This function returns size of tree rooted with given// root. It also sets "ans" as true if there is an edge// whose removal divides tree in two halves.// n is size of treestaticintcheckRec(Noderoot,intn,refboolans){// Base caseif(root==null)return0;// find the size of current subtreeintcnt=checkRec(root.left,n,refans)+checkRec(root.right,n,refans)+1;// If unlinking the current subtree from// main tree creates two trees of same size,// set ans to trueif(cnt==n-cnt)ans=true;// return the size of current // subtreereturncnt;}// This function mainly uses checkRec()staticboolcheck(Noderoot){// Count total nodes in given treeintn=countNodes(root);boolans=false;// Now recursively check all nodescheckRec(root,n,refans);returnans;}staticvoidMain(string[]args){// Binary tree // 5// / \// 1 6// / / \// 3 7 4Noderoot=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)){Console.WriteLine("True");}else{Console.WriteLine("False");}}}
JavaScript
// JavaScript program to check if there exist an edge whose// removal creates two trees of same sizeclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// To calculate size of tree with given rootfunctioncountNodes(root){if(root===null)return0;returncountNodes(root.left)+countNodes(root.right)+1;}// This function returns size of tree rooted with given// root. It also sets "ans" as true if there is an edge// whose removal divides tree in two halves.// n is size of treefunctioncheckRec(root,n,ans){// Base caseif(root===null)return0;// find the size of current subtreeletcnt=checkRec(root.left,n,ans)+checkRec(root.right,n,ans)+1;// If unlinking the current subtree from// main tree creates two trees of same size,// set ans to trueif(cnt===n-cnt)ans.val=true;// return the size of current subtreereturncnt;}// This function mainly uses checkRec()functioncheck(root){// Count total nodes in given treeletn=countNodes(root);letans={val:false};// Now recursively check all nodescheckRec(root,n,ans);returnans.val;}// Binary tree // 5// / \// 1 6// / / \// 3 7 4letroot=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)){console.log("True");}else{console.log("False");}