【leetcode】Binary Search Tree Iterator(middle)

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.

Calling next() will return the next smallest number in the BST.

Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.

思路:

说白了,就是把中序遍历拆成几个部分写。

我的代码:

class BSTIterator {
public:
    BSTIterator(TreeNode *root) {
        pCur = root;
        while(pCur != NULL)
        {
            v.push_back(pCur);
            pCur = pCur->left;
        }
    }

    /** @return whether we have a next smallest number */
    bool hasNext() {
        return (!v.empty() || NULL != pCur);
    }

    /** @return the next smallest number */
    int next() {
        int num;
        TreeNode * tmp = v.back();
        v.pop_back();
        num = tmp->val;
        pCur = tmp->right;
        while(pCur != NULL)
        {
            v.push_back(pCur);
            pCur = pCur->left;
        }
        return num;
    }
private:
    vector<TreeNode *> v;
    TreeNode * pCur;
};

大神更精简的代码: 经验,把相同功能的代码放在一起可以简化代码。

public class BSTIterator {

        Stack<TreeNode> stack =  null ;
        TreeNode current = null ;

        public BSTIterator(TreeNode root) {
              current = root;
              stack = new Stack<> ();
        }

        /** @return whether we have a next smallest number */
        public boolean hasNext() {
              return !stack.isEmpty() || current != null;
        }

            /** @return the next smallest number */
        public int next() {
            while (current != null) {
                stack.push(current);
                current = current.left ;
            }
            TreeNode t = stack.pop() ;
            current = t.right ;
            return t.val ;
        }
    }
时间: 2024-08-28 18:51:47

【leetcode】Binary Search Tree Iterator(middle)的相关文章

【LeetCode】Binary Search Tree Iterator (2 solutions)

Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in

【leetcode】Binary Search Tree Iterator

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses

[C++]LeetCode: 93 Binary Search Tree Iterator (经典题,非递归的中序遍历)

题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and u

【leetcode】Linked List Cycle II (middle)

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up:Can you solve it without using extra space? 思路: 做过,就当复习了. 先用快慢指针判断相交,关键是环开始点的获取. 用上图说明一下,设非环的部分长度为a(包括环的入口点), 环的长度为b(包括环的入口点). 快慢指针相交的位置为绿色的点,距离

【leetcode】Reverse Linked List II (middle)

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. 思路: 好困啊,脑子晕晕的. 转了半天AC了.但写的很罗嗦,要学习大神的写法. 注意翻转的写法. 用伪头部 大神14行简洁代码 L

【leetcode】Container With Most Water(middle)

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a containe

【leetcode】Minimum Size Subarray Sum(middle)

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

【leetcode】Evaluate Reverse Polish Notation(middle)

Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expression. Some examples: ["2", "1", "+", "3", "*"] -&g

? leetcode 173. Binary Search Tree Iterator 设计迭代器(搜索树)--------- java

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses