173. Binary Search Tree Iterator

建一个stack,最末的元素始终是最小的那个。

所以对于初始化,存到最左下角的那个节点。

对于找到next,就是存到,当前节点的右子节点的最左下角(如果有的话)。

 1 public class BSTIterator {
 2     Stack<TreeNode> stack;
 3
 4     public BSTIterator(TreeNode root) {
 5         stack = new Stack<TreeNode>();
 6         TreeNode cur = root;
 7         while(cur != null) {
 8             stack.push(cur);
 9             cur = cur.left;
10         }
11     }
12
13     /** @return whether we have a next smallest number */
14     public boolean hasNext() {
15         return !stack.isEmpty();
16     }
17
18     /** @return the next smallest number */
19     public int next() {
20         TreeNode cur = stack.pop();
21         int next = cur.val;
22         cur = cur.right;
23         while(cur != null) {
24             stack.push(cur);
25             cur = cur.left;
26         }
27         return next;
28     }
29 }
时间: 2024-10-04 07:55:35

173. Binary Search Tree Iterator的相关文章

173. Binary Search Tree Iterator - Unsolved

https://leetcode.com/problems/binary-search-tree-iterator/#/description 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 B

[email&#160;protected] [173] Binary Search Tree Iterator (InOrder traversal)

https://leetcode.com/problems/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: nex

leetcode 173. 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

[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 u

? 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

8.10 [LeetCode 173] Binary Search Tree Iterator

Question link 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)

[leedcode 173] 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

Java for LeetCode 173 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

173. 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

Leetcode solution 173: Binary Search Tree Iterator

Problem Statement 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. Example: BSTIterator iterator = new BSTIterator(ro