Skip to content

Commit 6f8965c

Browse files
author
YuChengKai
committed
BST 寻找最大最小值
1 parent 9191b73 commit 6f8965c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

DataStruct/dataStruct-zh.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,22 @@ breadthTraversal() {
371371
}
372372
```
373373

374+
接下来先介绍如何在树中寻找最小值或最大数。因为二分搜索树的特性,所以最小值一定在根节点的最左边,最大值相反
375+
376+
```js
377+
getMin() {
378+
return this._getMin(this.root).value
379+
}
380+
_getMin(node) {
381+
if (!node.left) return node
382+
return this._getMin(node.left)
383+
}
384+
getMax() {
385+
return this._getMax(this.root).value
386+
}
387+
_getMax(node) {
388+
if (!node.right) return node
389+
return this._getMin(node.right)
390+
}
391+
```
392+

0 commit comments

Comments
 (0)