@@ -133,7 +133,7 @@ var decodeString = function (s) {
133133##### [ 94. 二叉树的中序遍历] ( https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ )
134134
135135``` js
136- var inorderTraversal = function (root ) { // 递归写法
136+ var inorderTraversal = function (root ) { // 递归
137137 if (root === null ) {
138138 return []
139139 }
@@ -151,8 +151,200 @@ var inorderTraversal = function (root) { //递归写法
151151```
152152
153153``` js
154-
154+ var inorderTraversal = function (root ) {
155+ const stack = []
156+ const res = []
157+ let current = root
158+ while (stack .length > 0 || current) {
159+ while (current) {
160+ stack .push (current)
161+ current = current .left
162+ }
163+ current = stack .pop ()
164+ res .push (current .val )
165+ current = current .right
166+ }
167+ return res
168+ };
155169```
156170
157171##### [ 133.克隆图] ( https://leetcode-cn.com/problems/clone-graph/ )
158172
173+ 给你无向 ** [ 连通] ( https://baike.baidu.com/item/连通图/6460995?fr=aladdin ) ** 图中一个节点的引用,请你返回该图的 [ ** 深拷贝** ] ( https://baike.baidu.com/item/深拷贝/22785317?fr=aladdin ) (克隆)。
174+
175+ ``` js
176+ var cloneGraph = function (node ) {
177+ let visited = new Map ();
178+ return (function clone (node , visited ) {
179+ if (! node) {
180+ return null
181+ }
182+ if (visited .has (node)) {
183+ return visited .get (node)
184+ }
185+ const newNode = new Node (node .val )
186+ visited .set (node, newNode)
187+ for (let i = 0 ; i < node .neighbors .length ; i++ ) {
188+ newNode .neighbors [i] = clone (node .neighbors [i], visited)
189+ }
190+ return newNode
191+ })(node, visited)
192+ };
193+ ```
194+
195+ ##### [ 200. 岛屿数量] ( https://leetcode-cn.com/problems/number-of-islands/ )
196+
197+ 给你一个由 ` '1' ` (陆地)和 ` '0' ` (水)组成的的二维网格,请你计算网格中岛屿的数量。
198+
199+ ``` js
200+ var numIslands = function (grid ) {
201+ const nr = grid .length
202+ const nc = grid[0 ].length
203+ let resNum = 0
204+ for (let i = 0 ; i < nr; i++ ) {
205+ for (let j = 0 ; j < nc; j++ ) {
206+ if (grid[i][j] === ' 1' ) {
207+ resNum++
208+ dfs (grid, i, j)
209+ }
210+ }
211+ }
212+ return resNum
213+ };
214+
215+ const dfs = (grid , row , col ) => {
216+ const gr = grid .length
217+ const gc = grid[0 ].length
218+ if (row < 0 || col < 0 || row >= gr || col >= gc || grid[row][col] === ' 0' ) {
219+ return 0
220+ }
221+ grid[row][col] = ' 0'
222+ dfs (grid, row + 1 , col)
223+ dfs (grid, row - 1 , col)
224+ dfs (grid, row, col + 1 )
225+ dfs (grid, row, col - 1 )
226+ }
227+ ```
228+
229+ [ 84. 柱状图中最大的矩形] ( https://leetcode-cn.com/problems/largest-rectangle-in-histogram/ )
230+
231+ ``` js
232+
233+ ```
234+
235+ ### Queue 队列
236+
237+ ##### [ 232. 用栈实现队列] ( https://leetcode-cn.com/problems/implement-queue-using-stacks/ )
238+
239+ ``` js
240+ /**
241+ * Initialize your data structure here.
242+ */
243+ var MyQueue = function () {
244+ this .stack1 = []
245+ this .stack2 = []
246+ };
247+
248+ /**
249+ * Push element x to the back of queue.
250+ * @param {number} x
251+ * @return {void}
252+ */
253+ MyQueue .prototype .push = function (x ) {
254+ this .stack1 .push (x)
255+ };
256+
257+ /**
258+ * Removes the element from in front of queue and returns that element.
259+ * @return {number}
260+ */
261+ MyQueue .prototype .pop = function () {
262+ if (this .stack2 .length > 0 ) {
263+ return this .stack2 .pop ()
264+ } else {
265+ while (this .stack1 .length > 0 ) {
266+ this .stack2 .push (this .stack1 .pop ())
267+ }
268+ return this .stack2 .pop ()
269+ }
270+ };
271+
272+ /**
273+ * Get the front element.
274+ * @return {number}
275+ */
276+ MyQueue .prototype .peek = function () {
277+ if (this .stack2 .length > 0 ) {
278+ return this .stack2 [this .stack2 .length - 1 ]
279+ } else {
280+ while (this .stack1 .length > 0 ) {
281+ this .stack2 .push (this .stack1 .pop ())
282+ }
283+ return this .stack2 [this .stack2 .length - 1 ]
284+ }
285+ };
286+
287+ /**
288+ * Returns whether the queue is empty.
289+ * @return {boolean}
290+ */
291+ MyQueue .prototype .empty = function () {
292+ if (this .stack1 .length === 0 && this .stack2 .length === 0 ) {
293+ return true
294+ }
295+ return false
296+ };
297+ ```
298+
299+ ##### [ 542. 01 矩阵] ( https://leetcode-cn.com/problems/01-matrix/ )
300+
301+ ``` js
302+ var updateMatrix = function (matrix ) {
303+ const dirs = [[- 1 , 0 ], [1 , 0 ], [0 , - 1 ], [0 , 1 ]]
304+ const m = matrix .length , n = matrix[0 ].length
305+ const dist = new Array (m).fill (new Array (n).fill (0 ))
306+ const seen = new Array (m).fill (new Array (n).fill (false ))
307+ const queue = []
308+ for (let i = 0 ; i < m; i++ ) {
309+ for (let j = 0 ; j < n; j++ ) {
310+ if (matrix[i][j] === 0 ) {
311+ queue .push ([i, j])
312+ seen[i][j] = true
313+ }
314+ }
315+ }
316+ while (queue .length > 0 ) {
317+ const [i , j ] = queue .shift ()
318+ for (let d = 0 ; d < 4 ; d++ ) {
319+ const ni = i + dirs[d][0 ]
320+ const nj = j + dirs[d][1 ]
321+ if (ni >= 0 && ni < m && nj >= 0 && nj < n && ! seen[ni][nj]) {
322+ dist[ni][nj] = dist[i][j] + 1
323+ queue .push ([ni, nj])
324+ seen[ni][nj] = true
325+ }
326+ }
327+ }
328+ return dist
329+ };
330+ ```
331+
332+ ## 总结
333+
334+ - 熟悉栈的使用场景
335+ - 后入先出,保存临时值
336+ - 利用栈 DFS 深度搜索
337+ - 熟悉队列的使用场景
338+ - 利用队列 BFS 广度搜索
339+
340+ ## 练习
341+
342+ - [ min-stack] ( https://leetcode-cn.com/problems/min-stack/ )
343+ - [ evaluate-reverse-polish-notation] ( https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/ )
344+ - [ decode-string] ( https://leetcode-cn.com/problems/decode-string/ )
345+ - [ binary-tree-inorder-traversal] ( https://leetcode-cn.com/problems/binary-tree-inorder-traversal/ )
346+ - [ clone-graph] ( https://leetcode-cn.com/problems/clone-graph/ )
347+ - [ number-of-islands] ( https://leetcode-cn.com/problems/number-of-islands/ )
348+ - [ largest-rectangle-in-histogram] ( https://leetcode-cn.com/problems/largest-rectangle-in-histogram/ )
349+ - [ implement-queue-using-stacks] ( https://leetcode-cn.com/problems/implement-queue-using-stacks/ )
350+ - [ 01-matrix] ( https://leetcode-cn.com/problems/01-matrix/ )
0 commit comments