Given a binary tree containing many zero nodes, sink nodes having zero value at the bottom of the subtree rooted at that node. In other words, the output binary tree should not contain any node having zero value that is the parent of the node having a non-zero value.

For example, a binary tree shown on the left can be converted into a binary tree shown on the right.

 
Sink Nodes

Practice this problem

The idea is to process the tree nodes in a postorder manner, i.e., after fixing the left and right subtrees of a node, fix the node if it is zero. To fix a node, do something similar to the Heapify procedure of Heapsort algorithm. As the left and right subtree are already fixed, we can fix the binary tree rooted at the current node by moving the current node (containing zero) down the tree. We do so by comparing the node with its left and right child, swapping it with the non-zero child, and then recursively calling the procedure on the corresponding child. Repeat the process until a leaf node is reached or the subtree rooted at the current node contains all zeros.

The algorithm can be implemented as follows in C++, Java, and Python. As several binary trees can be constructed from one input, the solution would construct anyone.

C++


Download  Run Code

Output:

0 1 0 4 0 3 2

Java


Download  Run Code

Output:

0 1 0 4 0 3 2

Python


Download  Run Code

Output:

0 1 0 4 0 3 2

The time complexity of the above solution is O(n2), 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.