LeetCode – Binary Search Tree Iterator (Java)
Problem
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.
Java Solution
The key to solve this problem is understanding the features of BST. Here is an example BST.
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class BSTIterator { Stack<TreeNode> stack; public BSTIterator(TreeNode root) { stack = new Stack<TreeNode>(); while (root != null) { stack.push(root); root = root.left; } } public boolean hasNext() { return !stack.isEmpty(); } public int next() { TreeNode node = stack.pop(); int result = node.val; if (node.right != null) { node = node.right; while (node != null) { stack.push(node); node = node.left; } } return result; } } |
<pre><code> String foo = "bar"; </code></pre>
-
ajitesh kumar
-
shivam gupta
-
Phil Foster
-
Phil Foster
-
Cat Racket
-
David McGuire
-
Iram22
-
jaqen h’ghar
-
Subodh Karwa
-
Aditya Vutukuri
-
antonio081014
-
Defiler
-
Lookuptable
-
antonio081014
-
Abhi