Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
Example:
Input: 1 \ 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
Note:
- There are at least two nodes in this BST.
- This question is the same as 783: https://leetcode.com/problems/minimum-distance-between-bst-nodes/
给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。
- 由于是 BST 树,利用它有序的性质,中根遍历的结果是有序的。中根遍历过程中动态维护前后两个节点的差值,即可找到最小差值。
- 此题与第 783 题完全相同。
package leetcode import ( "math""github.com/halfrost/LeetCode-Go/structures" ) // TreeNode definetypeTreeNode= structures.TreeNode/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcgetMinimumDifference(root*TreeNode) int { res, nodes:=math.MaxInt16, -1dfsBST(root, &res, &nodes) returnres } funcdfsBST(root*TreeNode, res, pre*int) { ifroot==nil { return } dfsBST(root.Left, res, pre) if*pre!=-1 { *res=min(*res, abs(root.Val-*pre)) } *pre=root.ValdfsBST(root.Right, res, pre) } funcmin(a, bint) int { ifa>b { returnb } returna } funcabs(aint) int { ifa>0 { returna } return-a }