首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
二叉搜索树最小差值
[编程题]二叉搜索树最小差值
热度指数:1125
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一棵二叉搜索树,请你返回树中任意两节点之差的最小值。
数据范围:二叉树节点数满足
,二叉树的节点值满足
,保证每个节点的值都不同
示例1
输入
{2,1,3,#,#,#,4}
输出
1
示例2
输入
{3,1,5,#,#,#,9}
输出
2
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(11)
分享
纠错
提交结果有问题?
10个回答
14篇题解
开通博客
a_fighter_named_rudy
发表于 2022-10-25 18:30:19
解答:看到二叉搜索树,就思考二叉搜索树的中序遍历为有序数组是否有用,哎因为这题是求最小差值,所以能用该技巧,首先通过O(n)将二叉搜索树中序遍历结果存储到res数组中,然后因为res数组是上升的所以最小值只可能是i减i-1,所以for循环遍历结果数组得到最小值然后输出。总时间复杂度为O(n),空间复
展开全文
訾尤
发表于 2022-04-27 15:57:30
public class Solution { public int minDifference (TreeNode root) { List<Integer> list = new ArrayList<>(); // 首先进行中序遍历
展开全文
君无颜
发表于 2022-03-25 22:45:08
方法一(更容易理解) 先中序遍历取出二叉搜索树的升序数组(二叉搜索树都是左小右大) 然后遍历相减,比大小 c++实现 class Solution { public: vector<int> t; void mid(TreeNode* root){ i
展开全文
BlueProtocol
发表于 2022-10-08 13:45:02
public class MinDifference { private List<Integer> list = new ArrayList<Integer>(); public&n
展开全文
姐姐的遮阳伞
发表于 2022-04-06 21:18:44
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int v
展开全文
extern
发表于 2024-02-05 13:44:51
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {
展开全文
extern
发表于 2024-02-05 13:51:04
package main import ( "math" . "nc_tools" ) /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode
展开全文
ysrs
发表于 2022-04-14 00:11:59
* struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} *
展开全文
extern
发表于 2024-02-05 14:19:43
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option<Box<TreeNode>>, *
展开全文
fred-coder
发表于 2022-03-25 23:35:29
中序遍历后,求数组间差值 # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方
展开全文
问题信息
树
dfs
广度优先搜索(BFS)
难度:
10条回答
11收藏
2647浏览
热门推荐
通过挑战的用户
查看代码
半个西瓜半个夏
2022-09-16 09:43:12
牛客20434...
2022-09-14 00:54:25
Secret1...
2022-09-10 20:53:32
牛客50107...
2022-09-07 22:19:30
西方不胜
2022-09-07 09:50:53
相关试题
回路
dfs
评论
(51)
寻找道路
广度优先搜索(BFS)
NOIP复赛
评论
(0)
利用回溯法求下列不等式的所有整数解...
dfs
评论
(10)
来自
乐视2017秋招开发工程...
设某二叉树的先序遍历序列为abdg...
树
评论
(1)
2022 诺瓦科技 Perl re...
perl
System Verilog
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ public int minDifference (TreeNode root) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * }; */ class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ int minDifference(TreeNode* root) { // write code here } };
#coding:utf-8 # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @return int整型 # class Solution: def minDifference(self , root ): # write code here
using System; using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ public int minDifference (TreeNode root) { // write code here } }
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ function minDifference( root ) { // write code here } module.exports = { minDifference : minDifference };
val = $val; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ function minDifference( $root ) { // write code here }
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @return int整型 # class Solution: def minDifference(self , root: TreeNode) -> int: # write code here
package main //import "fmt" import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ func minDifference( root *TreeNode ) int { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ int minDifference(struct TreeNode* root ) { // write code here }
# class TreeNode # attr_accessor :val, :left, :right # def initialize(val, left = nil, right = nil) # @val, @left, @right = val, left, right # end # end # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @return int整型 # class Solution def minDifference(root) # write code here end end
/** * class TreeNode(var `val`: Int) { * var left: TreeNode = null * var right: TreeNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ def minDifference(root: TreeNode): Int = { // write code here } }
/** * class TreeNode(var `val`: Int) { * var left: TreeNode? = null * var right: TreeNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ fun minDifference(root: TreeNode?): Int { // write code here } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ public int minDifference (TreeNode root) { // write code here } }
/*class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ export function minDifference(root: TreeNode): number { // write code here }
/** * public class TreeNode { * public var val: Int * public var left: TreeNode? * public var right: TreeNode? * public init(_ val: Int=0, _ left: TreeNode?=nil, _ right: TreeNode?=nil) { * self.val = val * self.left = left * self.right = right * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ func minDifference ( _ root: TreeNode?) -> Int { // write code here } }
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option
>, * pub right: Option
>, * } * * impl TreeNode { * #[inline] * fn new(val: i32) -> Self { * TreeNode { * val: val, * left: None, * right: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return int整型 */ pub fn minDifference(&self, root: Option
>) -> i32 { // write code here } }
{2,1,3,#,#,#,4}
1
{3,1,5,#,#,#,9}
2