Given a Binary Tree having odd and even elements, sink all its odd valued nodes such that no node with odd value could be parent of node with even value. There can be multiple outputs for a given tree, we need to print one of them. It is always possible to convert a tree (Note that a node with even nodes and all odd nodes follows the rule)
Input : 1 / \ 2 3 Output 2 2 / \ OR / \ 1 3 3 1 Input : 1 / \ 5 8 / \ / \ 2 4 9 10 Output : 2 4 / \ / \ 4 8 OR 2 8 OR .. (any tree with / \ / \ / \ / \ same keys and 5 1 9 10 5 1 9 10 no odd is parent of even)
We strongly recommend you to minimize your browser and try this yourself first.
Basically, we need to swap odd value of a node with even value of one of its descendants. The idea is to traverse the tree in postorder fashion. Since we process in postorder, for each odd node encountered, its left and right subtrees are already balanced (sinked), we check if it’s an odd node and its left or right child has an even value. If even value is found, we swap the node’s data with that of even child node and call the procedure on the even child to balance the subtree. If both children have odd values, that means that all its descendants are odd.
Below is the implementation of the idea.
Level order traversal of modified tree 2 4 8 5 1 9 10
This article is contributed by Aditya Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Sink even nodes in Binary Tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Difference between sums of odd position and even position nodes for each level of a Binary Tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Difference between sum of even and odd valued nodes in a Binary Tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Sum of all odd nodes in the path connecting two given nodes
- Print levels with odd number of nodes and even number of nodes
- Check if a Binary Tree is an Even-Odd Tree or not
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Print the nodes at odd levels of a tree
- Count the nodes in the given tree whose sum of digits of weight is odd
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Count of all prime weight nodes between given nodes in the given Tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Convert a Generic Tree(N-array Tree) to Binary Tree
Improved By : SHUBHAMSINGH10