Convert a binary tree to a full tree by removing half nodes
Given a binary tree, convert it into a full tree by removing half nodes (remove nodes having one child). A full binary tree is a tree in which every node other than the leaves has two children.
For example, the binary tree shown on the left should be converted into the binary tree shown on the right.
The idea is to traverse the tree in a bottom-up fashion and convert the left and right subtree before processing a node. Then for each node,
- If it has two children or a leaf node, nothing needs to be done.
- If it has exactly one child, delete it and replace the node with the child node.
The algorithm can be implemented as follows in C++, Java, and Python:
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
#include <iostream> using namespace std; // Data structure to store a binary tree node struct Node { int data; Node *left, *right; Node(int data) { this->data = data; this->left = this->right = nullptr; } }; // Function to perform inorder traversal on the tree void inorder(Node* root) { if (root == nullptr) { return; } inorder(root->left); cout << root->data << " "; inorder(root->right); } // Function to check if a given node is a leaf node or not bool isLeaf(Node* node) { return (node->left == nullptr && node->right == nullptr); } // Function to convert a binary tree into a full tree by removing half nodes Node* truncate(Node* root) { // base case: empty tree if (root == nullptr) { return nullptr; } // recursively truncate the left subtree and subtree first root->left = truncate(root->left); root->right = truncate(root->right); // do nothing if the current node is a leaf node or has two children if ((root->left && root->right) || isLeaf(root)) { return root; } // if the current node has exactly one child, delete it and replace // it with the child node Node* child = (root->left) ? root->left: root->right; delete root; return child; } int main() { /* Construct the following tree 0 / \ / \ 1 2 / / / / 3 4 / / \ / / \ 5 6 7 */ Node* root = new Node(0); root->left = new Node(1); root->right = new Node(2); root->left->left = new Node(3); root->right->left = new Node(4); root->left->left->left = new Node(5); root->right->left->left = new Node(6); root->right->left->right = new Node(7); root = truncate(root); inorder(root); return 0; } |
Output:
5 0 6 4 7
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
// A class to store a binary tree node class Node { int data; Node left = null, right = null; Node(int data) { this.data = data; } } class Main { // Function to perform inorder traversal on the tree public static void inorder(Node root) { if (root == null) { return; } inorder(root.left); System.out.print(root.data + " "); inorder(root.right); } // Function to check if a given node is a leaf node or not public static boolean isLeaf(Node node) { return (node.left == null && node.right == null); } // Function to convert a binary tree into a full tree by removing half nodes public static Node truncate(Node root) { // base case: empty tree if (root == null) { return null; } // recursively truncate the left subtree and subtree first root.left = truncate(root.left); root.right = truncate(root.right); // do nothing if the current node is a leaf node or has two children if ((root.left != null && root.right != null) || isLeaf(root)) { return root; } // if the current node has exactly one child, delete it and // replace it with the child node Node child = (root.left != null) ? root.left: root.right; return child; } public static void main(String[] args) { /* Construct the following tree 0 / \ / \ 1 2 / / / / 3 4 / / \ / / \ 5 6 7 */ Node root = new Node(0); root.left = new Node(1); root.right = new Node(2); root.left.left = new Node(3); root.right.left = new Node(4); root.left.left.left = new Node(5); root.right.left.left = new Node(6); root.right.left.right = new Node(7); root = truncate(root); inorder(root); } } |
Output:
5 0 6 4 7
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# A class to store a binary tree node class Node: def __init__(self, data, left=None, right=None): self.data = data self.left = left self.right = right # Function to perform inorder traversal on the tree def inorder(root): if root is None: return inorder(root.left) print(root.data, end=' ') inorder(root.right) # Function to check if a given node is a leaf node or not def isLeaf(node): return node.left is None and node.right is None # Function to convert a binary tree into a full tree by removing half nodes def truncate(root): # base case: empty tree if root is None: return None # recursively truncate the left subtree and subtree first root.left = truncate(root.left) root.right = truncate(root.right) # do nothing if the current node is a leaf node or has two children if (root.left and root.right) or isLeaf(root): return root # if the current node has exactly one child, delete it and replace # it with the child node child = root.left if root.left else root.right return child if __name__ == '__main__': ''' Construct the following tree 0 / \ / \ 1 2 / / / / 3 4 / / \ / / \ 5 6 7 ''' root = Node(0) root.left = Node(1) root.right = Node(2) root.left.left = Node(3) root.right.left = Node(4) root.left.left.left = Node(5) root.right.left.left = Node(6) root.right.left.right = Node(7) root = truncate(root) inorder(root) |
Output:
5 0 6 4 7
The time complexity of the above solution is O(n), where n
is the total number of nodes in the binary tree. The program requires O(h) extra space for the call stack, where h
is the height of the tree.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)