We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。
注意:本题与 530:https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/ 相同
输入:root = [4,2,6,1,3] 输出:1
输入:root = [1,0,48,null,null,12,49] 输出:1
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
The text was updated successfully, but these errors were encountered:
因为是一个二叉搜索树,可以直接中序遍历获取排序后的结果
func minDiffInBST(root *TreeNode) int { var slice []int var dfs func(root *TreeNode) dfs = func (root *TreeNode) { if root == nil { return } dfs(root.Left) slice = append(slice, root.Val) dfs(root.Right) } dfs(root) ret := math.MaxInt64 for i := 0; i < len(slice)-1; i++ { if slice[i+1] - slice[i] < ret { ret = slice[i+1] - slice[i] } } return ret }
Sorry, something went wrong.
No branches or pull requests
给你一个二叉搜索树的根节点 root ,返回 树中任意两不同节点值之间的最小差值 。
注意:本题与 530:https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/ 相同
示例 1:
示例 2:
提示:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
The text was updated successfully, but these errors were encountered: