Increasing Order Search Tree
Problem
Given the root
of a binary search tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.
Constraints
- The number of nodes in the given tree will be in the range
[1, 100]
. 0 <= Node.val <= 1000
Solution
The problem Increasing Order Search Tree
can be solved by traversing the tree in an inorder and creating a new tree based on the problem instruction.
Implementation
static const int fast_io = []()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution
{
private:
TreeNode *head;
TreeNode *tail;
public:
void helper(TreeNode *node)
{
if (node == NULL)
return;
helper(node->left);
node->left = NULL;
tail = tail->right = node;
helper(node->right);
}
TreeNode *increasingBST(TreeNode *root)
{
head = tail = new TreeNode();
helper(root);
return head->right;
}
};