首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
恢复二叉搜索树
[编程题]恢复二叉搜索树
热度指数:14252
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32M,其他语言64M
算法知识视频讲解
二叉搜索树(BST)中的两个节点的值被错误地交换了,
请在不改变树的结构的情况下恢复这棵树。
备注;
用O(n)的空间解决这个问题的方法太暴力了,你能设计一个常数级空间复杂度的算法么?
示例 1:
输入: [1,3,null,null,2]
1 / 3 \ 2
输出: [3,1,null,null,2]
3 / 1 \ 2
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(21)
邀请回答
收藏(170)
分享
提交结果有问题?
70个回答
5篇题解
开通博客
卑微大橙子在线求offer
发表于 2020-05-29 21:27:51
传送门 题目 二叉搜索树(BST)中的两个节点被错误地交换了,需要在不改变树的结构的情况下恢复这棵树。 思路 一开始理解错了,以为是两个同一层的节点被交换了,后来发现是我想简单了,题意指的是任意的两个节点;那么如何找到这两个节点呢? 由二叉搜索树的概念,我们知道左子树所有节点的值都小于根节点的值,右
展开全文
华科不平凡
发表于 2020-08-23 00:25:05
二叉搜索树的中序遍历是有序的,如果二叉搜索树中两个节点被互换了,那么其中序遍历中必定有两个节点“错位”,因此中序遍历是解题的关键。中序遍历本身不难,但是题目要求常数级别的空间复杂度,因此想到了线索二叉树。 总结下来两种思路: 空间复杂度为O(n)——线索二叉树 空间复杂度为O(logn)——递归,
展开全文
一叶浮尘
发表于 2020-04-11 13:45:46
树练习的最后一道题目,说实话看的不是很懂,也没有太多的思路
O-Precedence
发表于 2021-09-09 16:29:20
class Solution { private TreeNode t1,t2,pre; public void recoverTree(TreeNode root) { inOrder(root); int temp = t1.val;
展开全文
牛客487629784号
发表于 2022-01-27 18:45:08
* Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left
展开全文
问题信息
树
难度:
70条回答
170收藏
23644浏览
热门推荐
通过挑战的用户
查看代码
牛客84407...
2022-09-04 10:27:18
Varus20...
2022-08-16 09:43:07
Lilisten
2022-07-24 17:11:13
sunshin...
2022-07-07 13:46:03
DamonGuan
2022-07-02 00:16:56
相关试题
() 通过计算机网络给 () 发送...
网络基础
评论
(1)
无源晶振起振电容容量选择方法
元器件
评论
(1)
如果让你策划设计一个影片评论功能的...
竞品研究
评论
(1)
手写代码:循环链表插入元素
评论
(1)
计算二元分类的Jaccard指数
机器学习
评论
(1)
恢复二叉搜索树
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public void recoverTree(TreeNode root) { } }
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void recoverTree(TreeNode *root) { } };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return void # class Solution: def recoverTree(self , root ): # write code here
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 * @return void */ function recoverTree( root ) { // write code here } module.exports = { recoverTree : recoverTree };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return void # class Solution: def recoverTree(self , root ): # write code here
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * * @param root TreeNode类 * @return void */ func recoverTree( root *TreeNode ) { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * * @param root TreeNode类 * @return void */ void recoverTree(struct TreeNode* root ) { // write code here }