1. 程式人生 > >[Java]LeetCode173. 二叉搜尋樹迭代器 | Binary Search Tree Iterator

[Java]LeetCode173. 二叉搜尋樹迭代器 | 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 O(h) memory, where h

 is the height of the tree.


實現一個二叉搜尋樹迭代器。你將使用二叉搜尋樹的根節點初始化迭代器。

呼叫 next() 將返回二叉搜尋樹中的下一個最小的數。

注意: next() 和hasNext() 操作的時間複雜度是O(1),並使用 O(h) 記憶體,其中 是樹的高度。


1ms

 1 public class BSTIterator {
 2     // flatten tree on the go, O(N) time, O(1) space
3 TreeNode head; 4 5 public BSTIterator(TreeNode root) { 6 head = root; 7 } 8 9 /** @return whether we have a next smallest number */ 10 public boolean hasNext() { 11 return head != null; 12 } 13 14 /** @return the next smallest number */
15 public int next() { 16 while (head.left != null) { 17 TreeNode left = head.left, tmp = left; 18 while (tmp.right != null && tmp.right != head) 19 tmp = tmp.right; 20 if (tmp.right == head) { 21 tmp.right = null; 22 break; 23 } else { 24 tmp.right = head; 25 head = left; 26 } 27 } 28 int ret = head.val; 29 head = head.right; 30 return ret; 31 } 32 }

2ms

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 
11 public class BSTIterator {
12     ArrayList<Integer> orderedList;
13     int index;
14     private void flatten(TreeNode node) {
15         if (node == null)
16             return;
17         flatten(node.left);
18         orderedList.add(node.val);
19         flatten(node.right);
20     }
21     
22     public BSTIterator(TreeNode root) {
23         orderedList = new ArrayList<>();
24         index = 0;
25         flatten(root);
26     }
27 
28     /** @return whether we have a next smallest number */
29     public boolean hasNext() {
30         return index < orderedList.size();
31     }
32 
33     /** @return the next smallest number */
34     public int next() {
35         return orderedList.get(index++);
36     }
37 }
38 
39 /**
40  * Your BSTIterator will be called like this:
41  * BSTIterator i = new BSTIterator(root);
42  * while (i.hasNext()) v[f()] = i.next();
43  */

3ms

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 
11 public class BSTIterator {
12     Stack<TreeNode> st;
13 
14     public BSTIterator(TreeNode root) {
15         st = new Stack<TreeNode>();
16         while (root != null) {
17             st.push(root);
18             root = root.left;
19         }
20         
21     }
22 
23     /** @return whether we have a next smallest number */
24     public boolean hasNext() {
25         return (!st.isEmpty());
26         
27     }
28 
29     /** @return the next smallest number */
30     public int next() {
31         if (st.isEmpty())
32             return -1;
33         TreeNode ret = st.pop();
34         TreeNode curr = ret;
35         if (curr.right != null) {
36             curr = curr.right;
37             while(curr != null) {
38                 st.push(curr);
39                 curr = curr.left;
40             }
41         }
42         return ret.val;
43     }
44 }
45 
46 /**
47  * Your BSTIterator will be called like this:
48  * BSTIterator i = new BSTIterator(root);
49  * while (i.hasNext()) v[f()] = i.next();
50  */

4ms

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 
11 public class BSTIterator {
12 
13     private Stack<TreeNode> stack;
14     public BSTIterator(TreeNode root) {
15         stack = new Stack<>();
16         TreeNode cur = root;
17         while(cur != null){
18             stack.push(cur);
19             if(cur.left != null)
20                 cur = cur.left;
21             else
22                 break;
23         }
24     }
25 
26     /** @return whether we have a next smallest number */
27     public boolean hasNext() {
28         return !stack.isEmpty();
29     }
30 
31     /** @return the next smallest number */
32     public int next() {
33         TreeNode node = stack.pop();
34         TreeNode cur = node;
35         // traversal right branch
36         if(cur.right != null){
37             cur = cur.right;
38             while(cur != null){
39                 stack.push(cur);
40                 if(cur.left != null)
41                     cur = cur.left;
42                 else
43                     break;
44             }
45         }
46         return node.val;
47     }
48 }
49 
50 /**
51  * Your BSTIterator will be called like this:
52  * BSTIterator i = new BSTIterator(root);
53  * while (i.hasNext()) v[f()] = i.next();
54  */

5ms

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 
11 public class BSTIterator {
12     //test case: [5,3,6,2,4,null,null,1]
13     private Stack<TreeNode> stack;
14 
15     public BSTIterator(TreeNode root) {
16         stack = new Stack<>();
17         TreeNode curr = root;
18         
19         while (curr!= null)
20         {
21             stack.push(curr);
22             curr = curr.left;
23         }
24     }
25 
26     /** @return whether we have a next smallest number */
27     public boolean hasNext() {
28         return !stack.isEmpty();
29     }
30 
31     /** @return the next smallest number */
32     public int next() {
33         TreeNode node = stack.pop();
34         
35         
36         if (node.right != null)
37         {
38             TreeNode curr = node.right;
39             while (curr!= null)
40             {
41                 stack.push(curr);
42                 curr = curr.left;
43             }
44         }
45         
46         return node.val;
47     }
48 }
49 
50 /**
51  * Your BSTIterator will be called like this:
52  * BSTIterator i = new BSTIterator(root);
53  * while (i.hasNext()) v[f()] = i.next();
54  */

6ms

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 
11 public class BSTIterator {
12   
13   private TreeNode current;
14   private Stack<TreeNode> stack;
15 
16   public BSTIterator(TreeNode root) {
17     stack = new Stack<>();
18     current = root;
19   }
20 
21   /** @return whether we have a next smallest number */
22   public boolean hasNext() {
23     return !stack.isEmpty() || current != null;
24   }
25 
26   /** @return the next smallest number */
27   public int next() {
28     while (current != null) {
29       stack.push(current);
30       current = current.left;
31     }
32     
33     current = stack.peek().right;
34     return stack.pop().val;
35   }
36 }
37 
38 /**
39  * Your BSTIterator will be called like this:
40  * BSTIterator i = new BSTIterator(root);
41  * while (i.hasNext()) v[f()] = i.next();
42  */