Trim given Binary Tree for any subtree containing only 0s
Last Updated :
28 Jan, 2022
Given a Binary tree, the task is to trim this tree for any subtree containing only 0s.
Examples:
Input:
1
\
0
/ \
0 1
Output:
1
\
0
\
1
Explanation:
The subtree shown as bold below
does not contain any 1.
Hence it can be trimmed.
1
\
0
/ \
0 1
Input:
1
/ \
1 0
/ \ / \
1 1 0 1
/
0
Output:
1
/ \
1 0
/ \ \
1 1 1
Input:
1
/ \
0 1
/ \ / \
0 0 0 1
Output:
1
\
1
\
1
Approach: The given problem can be solved using post-order traversal. The idea is to return null node to the parent if both left and right subtree is null and value of current node is 0. This removes the subtrees which do not contain even a single 1. Follow the steps below to solve the problem:
- If the root is null, we simply return null.
- Call the function recursively on both left and right subtrees
- If left subtree and right subtree returns null and current node’s value is 0 return null
- Else return the current node itself
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class TreeNode {
public :
int data;
TreeNode* left;
TreeNode* right;
TreeNode( int val)
{
data = val;
left = NULL;
right = NULL;
}
};
void inorderPrint(TreeNode* root)
{
if (root == NULL)
return ;
inorderPrint(root->left);
cout << root->data << " " ;
inorderPrint(root->right);
}
TreeNode* TrimTree(TreeNode* root)
{
if (!root)
return nullptr;
root->left = TrimTree(root->left);
root->right = TrimTree(root->right);
if (root->data == 0 && root->left == nullptr
&& root->right == nullptr) {
return nullptr;
}
return root;
}
int main()
{
TreeNode* root = new TreeNode(1);
root->left = new TreeNode(0);
root->right = new TreeNode(1);
root->left->left = new TreeNode(0);
root->left->right = new TreeNode(0);
root->right->left = new TreeNode(0);
root->right->right = new TreeNode(1);
TreeNode* ReceivedRoot = TrimTree(root);
cout << endl;
inorderPrint(ReceivedRoot);
}
|
Java
class GFG{
static class TreeNode {
int data;
TreeNode left;
TreeNode right;
TreeNode( int val)
{
data = val;
left = null ;
right = null ;
}
};
static void inorderPrint(TreeNode root)
{
if (root == null )
return ;
inorderPrint(root.left);
System.out.print(root.data+ " " );
inorderPrint(root.right);
}
static TreeNode TrimTree(TreeNode root)
{
if (root== null )
return null ;
root.left = TrimTree(root.left);
root.right = TrimTree(root.right);
if (root.data == 0 && root.left == null
&& root.right == null ) {
return null ;
}
return root;
}
public static void main(String[] args)
{
TreeNode root = new TreeNode( 1 );
root.left = new TreeNode( 0 );
root.right = new TreeNode( 1 );
root.left.left = new TreeNode( 0 );
root.left.right = new TreeNode( 0 );
root.right.left = new TreeNode( 0 );
root.right.right = new TreeNode( 1 );
TreeNode ReceivedRoot = TrimTree(root);
System.out.println();
inorderPrint(ReceivedRoot);
}
}
|
Python3
class TreeNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None ;
def inorderPrint(root):
if (root = = None ):
return ;
inorderPrint(root.left);
print (root.data, end = " " );
inorderPrint(root.right);
def TrimTree(root):
if (root = = None ):
return None ;
root.left = TrimTree(root.left);
root.right = TrimTree(root.right);
if (root.data = = 0 and root.left = = None and root.right = = None ):
return None ;
return root;
if __name__ = = '__main__' :
root = TreeNode( 1 );
root.left = TreeNode( 0 );
root.right = TreeNode( 1 );
root.left.left = TreeNode( 0 );
root.left.right = TreeNode( 0 );
root.right.left = TreeNode( 0 );
root.right.right = TreeNode( 1 );
ReceivedRoot = TrimTree(root);
print ();
inorderPrint(ReceivedRoot);
|
C#
using System;
public class GFG{
class TreeNode {
public int data;
public TreeNode left;
public TreeNode right;
public TreeNode( int val)
{
data = val;
left = null ;
right = null ;
}
};
static void inorderPrint(TreeNode root)
{
if (root == null )
return ;
inorderPrint(root.left);
Console.Write(root.data+ " " );
inorderPrint(root.right);
}
static TreeNode TrimTree(TreeNode root)
{
if (root== null )
return null ;
root.left = TrimTree(root.left);
root.right = TrimTree(root.right);
if (root.data == 0 && root.left == null
&& root.right == null ) {
return null ;
}
return root;
}
public static void Main(String[] args)
{
TreeNode root = new TreeNode(1);
root.left = new TreeNode(0);
root.right = new TreeNode(1);
root.left.left = new TreeNode(0);
root.left.right = new TreeNode(0);
root.right.left = new TreeNode(0);
root.right.right = new TreeNode(1);
TreeNode ReceivedRoot = TrimTree(root);
Console.WriteLine();
inorderPrint(ReceivedRoot);
}
}
|
Javascript
<script>
class TreeNode {
constructor( val)
{
this .data = val;
this .left = null ;
this .right = null ;
}
};
function inorderPrint( root)
{
if (root == null )
return ;
inorderPrint(root.left);
document.write(root.data + " " );
inorderPrint(root.right);
}
function TrimTree( root)
{
if (!root)
return null ;
root.left = TrimTree(root.left);
root.right = TrimTree(root.right);
if (root.data == 0 && root.left == null
&& root.right == null ) {
return null ;
}
return root;
}
let root = new TreeNode(1);
root.left = new TreeNode(0);
root.right = new TreeNode(1);
root.left.left = new TreeNode(0);
root.left.right = new TreeNode(0);
root.right.left = new TreeNode(0);
root.right.right = new TreeNode(1);
let ReceivedRoot = TrimTree(root);
document.write('<br>')
inorderPrint(ReceivedRoot);
</script>
|
Time Complexity: O(N), where N is the number of nodes in the tree.
Auxiliary Space: O(H), the recursion call stack can be as large as the height H of the tree. In the worst-case scenario, H = N, when the tree is skewed.
Share your thoughts in the comments
Please Login to comment...