Write a program that converts a given tree to its Double tree. To create Double tree of the given tree, create a new duplicate for each node, and insert the duplicate as the left child of the original node.
So the tree…
2 / \ 1 3
is changed to…
2 / \ 2 3 / / 1 3 / 1
And the tree
1 / \ 2 3 / \ 4 5
is changed to
1 / \ 1 3 / / 2 3 / \ 2 5 / / 4 5 / 4
Recursively convert the tree to double tree in postorder fashion. For each node, first convert the left subtree of the node, then right subtree, finally create a duplicate node of the node and fix the left child of the node and left child of left child.
Original tree is : 4 2 5 1 3 Inorder traversal of double tree is : 4 4 2 2 5 5 1 1 3 3
Time Complexity: O(n) where n is the number of nodes in the tree.
Please write comments if you find any bug in above code/algorithm, or find other ways to solve the same problem.
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Convert a given Binary tree to a tree that holds Logical AND property
- Sub-tree with minimum color difference in a 2-coloured tree
- Cartesian tree from inorder traversal | Segment Tree
- Convert a Binary Tree into its Mirror Tree
- Create a mirror tree from the given binary tree
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Check if the given n-ary tree is a binary tree
- Minimum swap required to convert binary tree to binary search tree
Improved By : rathbhupendra