-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
129 lines (122 loc) · 3.26 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* @lc app=leetcode id=173 lang=javascript
*
* [173] Binary Search Tree Iterator
*
* https://leetcode.com/problems/binary-search-tree-iterator/description/
*
* algorithms
* Medium (46.68%)
* Total Accepted: 261.3K
* Total Submissions: 493.4K
* Testcase Example: '["BSTIterator","next","next","hasNext","next","hasNext","next","hasNext","next","hasNext"]\n' +
'[[[7,3,15,null,null,9,20]],[null],[null],[null],[null],[null],[null],[null],[null],[null]]'
*
* 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(root);
* iterator.next(); // return 3
* iterator.next(); // return 7
* iterator.hasNext(); // return true
* iterator.next(); // return 9
* iterator.hasNext(); // return true
* iterator.next(); // return 15
* iterator.hasNext(); // return true
* iterator.next(); // return 20
* iterator.hasNext(); // return false
*
*
*
*
* Note:
*
*
* next() and hasNext() should run in average O(1) time and uses O(h) memory,
* where h is the height of the tree.
* You may assume that next() call will always be valid, that is, there will be
* at least a next smallest number in the BST when next() is called.
*
*
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
*/
var BSTIterator = function(root) {
this.stack = [];
let node = root;
while (node) {
this.stack.push(node);
node = node.left;
}
this.nextNode = this.stack.pop();
};
/**
* @return the next smallest number
* @return {number}
*/
BSTIterator.prototype.next = function() {
const nextVal = this.nextNode.val;
let node = this.nextNode;
if (node.right) {
node = node.right;
while (node) {
this.stack.push(node);
node = node.left;
}
this.nextNode = this.stack.pop();
} else {
this.nextNode = this.stack.pop();
}
return nextVal;
};
/**
* @return whether we have a next smallest number
* @return {boolean}
*/
BSTIterator.prototype.hasNext = function() {
return !!this.nextNode || this.stack.length !==0;
};
const { make_tree } = require('../utils');
const [ root ] = make_tree([7,3,15,null,null,9,20], [0]);
const iterator = new BSTIterator(root);
console.log(iterator.next()); // return 3
console.log(iterator.next()); // return 7
console.log(iterator.hasNext()); // return true
console.log(iterator.next()); // return 9
console.log(iterator.hasNext()); // return true
console.log(iterator.next()); // return 15
console.log(iterator.hasNext()); // return true
console.log(iterator.next()); // return 20
console.log(iterator.hasNext()); // return false
/**
* Your BSTIterator object will be instantiated and called as such:
* var obj = new BSTIterator(root)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/
module.exports = {
id:'173',
title:'Binary Search Tree Iterator',
url:'https://leetcode.com/problems/binary-search-tree-iterator/description/',
difficulty:'Medium',
}