KatInterview

933. Increasing Order Search Tree

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

Method: In-order Traversal with In-place Rewiring

The problem asks us to rearrange a Binary Search Tree (BST) into a right-skewed tree, where nodes appear in increasing order. This structure is essentially a linked list where each node's right pointer points to the next node in the sequence, and the left pointer is always null.

Solution

Increasing Order Search Tree in Python

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
from typing import Optional

class Solution:
    def increasingBST(self, root: TreeNode) -> TreeNode:
        # Sentinel node to hold the new root, simplifying head management.
        sentinel = TreeNode(0)
        # self.currentNode will track the tail of the new right-skewed tree.
        self.currentNode = sentinel
        
        # Perform in-order traversal to rewire the tree.
        self.inorder(root)
        
        # The new root is the right child of the sentinel.
        return sentinel.right

    def inorder(self, node: Optional[TreeNode]):
        # Base case for recursion.
        if not node:
            return
        
        # 1. Traverse the left subtree.
        self.inorder(node.left)
        
        # 2. Process the current node.
        # The original node's left child must be set to null.
        node.left = None
        # Link the previous node (self.currentNode) to the current node.
        self.currentNode.right = node
        # Move the self.currentNode pointer to the current node.
        self.currentNode = node
        
        # 3. Traverse the right subtree.
        self.inorder(node.right)

Increasing Order Search Tree in Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    private TreeNode currentNode;

    public TreeNode increasingBST(TreeNode root) {
        // Sentinel node to hold the new root, simplifying head management.
        TreeNode sentinel = new TreeNode(0);
        // currentNode will track the tail of the new right-skewed tree.
        currentNode = sentinel;

        // Perform in-order traversal to rewire the tree.
        inorder(root);

        // The new root is the right child of the sentinel.
        return sentinel.right;
    }

    private void inorder(TreeNode node) {
        // Base case for recursion.
        if (node == null) {
            return;
        }

        // 1. Traverse the left subtree.
        inorder(node.left);

        // 2. Process the current node.
        // The original node's left child must be set to null.
        node.left = null;
        // Link the previous node (currentNode) to the current node.
        currentNode.right = node;
        // Move the currentNode pointer to the current node.
        currentNode = node;

        // 3. Traverse the right subtree.
        inorder(node.right);
    }
}

Increasing Order Search Tree in C++

/**
 * 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* currentNode;

public:
    TreeNode* increasingBST(TreeNode* root) {
        // Sentinel node to hold the new root, simplifying head management.
        TreeNode* sentinel = new TreeNode(0);
        // currentNode will track the tail of the new right-skewed tree.
        currentNode = sentinel;
        
        // Perform in-order traversal to rewire the tree.
        inorder(root);
        
        // The new root is the right child of the sentinel.
        return sentinel->right;
    }

private:
    void inorder(TreeNode* node) {
        // Base case for recursion.
        if (node == nullptr) {
            return;
        }
        
        // 1. Traverse the left subtree.
        inorder(node->left);
        
        // 2. Process the current node.
        // The original node's left child must be set to null.
        node->left = nullptr;
        // Link the previous node (currentNode) to the current node.
        currentNode->right = node;
        // Move the currentNode pointer to the current node.
        currentNode = node;
        
        // 3. Traverse the right subtree.
        inorder(node->right);
    }
};

Increasing Order Search Tree in TypeScript

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function increasingBST(root: TreeNode | null): TreeNode | null {
    // Sentinel node to hold the new root, simplifying head management.
    const sentinel = new TreeNode(0);
    // currentNode will track the tail of the new right-skewed tree.
    let currentNode: TreeNode = sentinel;

    // Helper function to perform in-order traversal and rewire the tree.
    function inorder(node: TreeNode | null): void {
        // Base case for recursion.
        if (!node) {
            return;
        }

        // 1. Traverse the left subtree.
        inorder(node.left);

        // 2. Process the current node.
        // The original node's left child must be set to null.
        node.left = null;
        // Link the previous node (currentNode) to the current node.
        currentNode.right = node;
        // Move the currentNode pointer to the current node.
        currentNode = node;

        // 3. Traverse the right subtree.
        inorder(node.right);
    }

    // Start the traversal from the root.
    inorder(root);

    // The new root is the right child of the sentinel.
    return sentinel.right;
}

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.