↓Skip to main content
  1. LeetCode/

LeetCode 897: Increasing Order Search Tree

·1 min· ·
LeetCode Easy Stack Tree Depth-First-Search Binary-Search-Tree
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-09-25

  • Submissions: Accepted ×2 → Runtime Error ×3 → Wrong Answer ×4 → Accepted ×2

Approach
#

In-order yields nodes in ascending order (Tree Template 3). With a dummy and a tail pointer, attach each node at tail.right, clear its left, and advance tail. Return dummy.right.

My Attempts
#

The first versions read attributes of None. Later errors were all about the dummy: moving the dummy itself as the tail, so the final dummy.right was the last node’s right (None); [7] returned nothing and [6, 0] kept only [6].

Solution
#

class Solution:
    def increasingBST(self, root: TreeNode) -> TreeNode:
        dummy = tail = TreeNode()
        stack, cur = [], root
        while cur or stack:
            while cur:
                stack.append(cur)
                cur = cur.left
            cur = stack.pop()
            cur.left = None
            tail.right = tail = cur
            cur = cur.right
        return dummy.right

Complexity
#

Time O(n), space O(h).

Mistakes to Avoid
#

  • Move tail; keep dummy in place to return the head.
  • Clearing cur.left is safe: its left subtree is already done.

Interview Explanation
#

An iterative in-order walk gives nodes in ascending order; I attach each to the tail’s right, clear its left, and return the dummy’s right. Time O(n).

Related

LeetCode 783: Minimum Distance Between BST Nodes
·2 mins
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Search-Tree
LeetCode 783 note: a BST’s in-order is ascending, so the minimum difference is between in-order neighbors; remember the previous value and compare
LeetCode 589: N-ary Tree Preorder Traversal
·2 mins
LeetCode Easy Stack Tree Depth-First-Search
LeetCode 589 note: n-ary pre-order records the node then each child in order; flatten recursive results with extend, not by appending a list
LeetCode 590: N-ary Tree Postorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search
LeetCode 590 note: post-order is each child then the node; iteratively, run a root-then-children-right-to-left pre-order and reverse the whole result
LeetCode 501: Find Mode in Binary Search Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 501 note: a BST’s in-order is sorted, so equal values are contiguous; count the current run during in-order, resetting the answer on a new maximum and appending on a tie
LeetCode 145: Binary Tree Postorder Traversal
·2 mins
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 145 note: iterative post-order pushes a node back with a visited flag on its first pop, then the right and left children; record it on the second pop
LeetCode 144: Binary Tree Preorder Traversal
·1 min
LeetCode Easy Stack Tree Depth-First-Search Binary-Tree
LeetCode 144 note: iterative pre-order traversal records on pop and pushes the right child before the left so the left is handled first