mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
JavaScript版本的《二叉搜索树的最小绝对差》
This commit is contained in:
@ -256,6 +256,39 @@ func findMIn(root *TreeNode,res *[]int){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
JavaScript版本
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
/**
|
||||||
|
* Definition for a binary tree node.
|
||||||
|
* function TreeNode(val, left, right) {
|
||||||
|
* this.val = (val===undefined ? 0 : val)
|
||||||
|
* this.left = (left===undefined ? null : left)
|
||||||
|
* this.right = (right===undefined ? null : right)
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* @param {TreeNode} root
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
var getMinimumDifference = function (root) {
|
||||||
|
let arr = [];
|
||||||
|
const buildArr = (root) => {
|
||||||
|
if (root) {
|
||||||
|
buildArr(root.left);
|
||||||
|
arr.push(root.val);
|
||||||
|
buildArr(root.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
buildArr(root);
|
||||||
|
let diff = arr[arr.length - 1];
|
||||||
|
for (let i = 1; i < arr.length; ++i) {
|
||||||
|
if (diff > arr[i] - arr[i - 1])
|
||||||
|
diff = arr[i] - arr[i - 1];
|
||||||
|
}
|
||||||
|
return diff;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
Reference in New Issue
Block a user