Input: K = 0, 5 / \ 3 6 Output: 3 \ 5 \ 6 Input: K = 1, 2 / \ 1 3 Output: 3 \ 2 \ 1
- The key observation in the problem is that the first node of the skewed tree will be the extreme left or extreme right node of the BST for increasing order and decreasing order respectively.
For Increasing Order we need to do the Inorder Traversal, as the inorder traversal of a BST provides us the increasing sequence of the node values. Hence, the order of traversal at every node will be:
- Left node: Recurse to its left node if it exists, to get smaller value.
- Root node: After the complete traversal of its left node/subtree, connect the previous node of the skewed tree to the root node.
- Right node: Recurse to the right node if it exists, for larger values.
- For Decreasing Order, the order of traversal at every node will be:
- Right node: Recurse to its right node if it exists, to get larger values.
- Root node: After the complete traversal of its right node/subtree, connect the previous node of the skewed tree to the root node.
- Left node: Recurse to the left node/subtree for smaller values.
- Similarly, by keeping track of the previous node we can traverse the Binary search tree according to the order needed and form the skewed tree.
Below is the implementation of the above approach:
3 5 6
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.
- Check whether a given binary tree is skewed binary tree or not?
- Skewed Binary Tree
- Minimum swap required to convert binary tree to binary search tree
- Ways to color a skewed tree such that parent and child have different colors
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Convert a Binary Tree into its Mirror Tree
- Binary Tree to Binary Search Tree Conversion
- Binary Tree to Binary Search Tree Conversion using STL set
- Difference between Binary Tree and Binary Search Tree
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten binary tree in order of post-order traversal
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree into Doubly Linked List in spiral fashion
- Flatten BST to sorted list | Decreasing order
- Number of ways to divide a given number as a set of integers in decreasing order
- Count the Number of Binary Search Trees present in a Binary 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)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.