Formatted question description: https://leetcode.ca/all/897.html

897. Increasing Order Search Tree (Easy)

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.

 

Example 1:

Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]

Example 2:

Input: root = [5,1,7]
Output: [1,null,5,null,7]

 

Constraints:

  • The number of nodes in the given tree will be in the range [1, 100].
  • 0 <= Node.val <= 1000

Related Topics:
Tree, Depth-first Search, Recursion

Solution 1. In-order Traversal

// OJ: https://leetcode.com/problems/increasing-order-search-tree

// Time: O(N)
// Space: O(H)
class Solution {
private:
    TreeNode *prev;
    void inorder(TreeNode *root) {
        if (!root) return;
        inorder(root->left);
        root->left = NULL;
        prev->right = root;
        prev = root; 
        inorder(root->right);
    }
public:
    TreeNode* increasingBST(TreeNode* root) {
        TreeNode head;
        prev = &head;
        inorder(root);
        return head.right;
    }
};

Solution 2. Post-order Traversal

// OJ: https://leetcode.com/problems/increasing-order-search-tree/

// Time: O(N)
// Space: O(H)
class Solution {
    pair<TreeNode*, TreeNode*> dfs(TreeNode* root) {
        TreeNode *head = root, *tail = root;
        if (root->left) {
            auto [leftHead, leftTail] = dfs(root->left);
            head = leftHead;
            leftTail->right = root;
            root->left = NULL;
        }
        if (root->right) {
            auto [rightHead, rightTail] = dfs(root->right);
            root->right = rightHead;
            tail = rightTail;
        }
        return { head, tail };
    }
public:
    TreeNode* increasingBST(TreeNode* root) {
        return dfs(root).first;
    }
};

Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode increasingBST(TreeNode root) {
        if (root == null || root.left == null && root.right == null)
            return root;
        List<Integer> inorderTraversal = inorderTraversal(root);
        TreeNode newRoot = new TreeNode(inorderTraversal.get(0));
        TreeNode temp = newRoot;
        int size = inorderTraversal.size();
        for (int i = 1; i < size; i++) {
            TreeNode node = new TreeNode(inorderTraversal.get(i));
            temp.right = node;
            temp = temp.right;
        }
        return newRoot;
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> inorderTraversal = new ArrayList<Integer>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode node = root;
        while (!stack.isEmpty() || node != null) {
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
            TreeNode visitNode = stack.pop();
            inorderTraversal.add(visitNode.val);
            node = visitNode.right;
        }
        return inorderTraversal;
    }
}

All Problems

All Solutions