leetcode 897. 递增顺序搜索树(中序遍历)

给你一棵二叉搜索树,请你 按中序遍历 将其重新排列为一棵递增顺序搜索树,使树中最左边的节点成为树的根节点,并且每个节点没有左子节点,只有一个右子节点。

示例 1:

输入:root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
输出:[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
示例 2:

输入:root = [5,1,7]
输出:[1,null,5,null,7]
提示:

树中节点数的取值范围是 [1, 100]
0 <= Node.val <= 1000

解题思路

二叉搜索树中序遍历的次序就是由小到大的,因此我们在中序遍历的过程中,可以新建一个头节点,然后按中序遍历的顺序,将题目中给出的二叉树节点,一个一个地连在新树上面。在中序遍历的时候,修改节点指向就可以实现。具体地,当我们遍历到一个节点时,把它的左孩子设为空,并将其本身作为上一个遍历到的节点的右孩子。这里需要有一些想象能力。递归遍历的过程中,由于递归函数的调用栈保存了节点的引用。

下面的代码中,使用了 dummy 节点,它一般在链表题中出现。在链表题目中,我们为了防止链表的头结点发生变化之后,不好维护头结点,我们设置 dummy 从而保证头结点不变。这个题目中设置了 dummy ,从而保证了在新的树中,dummy 是根节点,不需要在递归的时候选择根节点,最终返回的时候,要返回的是 dummy.right。

代码

/*** Definition for a binary tree node.* type TreeNode struct {*     Val int*     Left *TreeNode*     Right *TreeNode* }*/var t = new(TreeNode)
func increasingBST(root *TreeNode) *TreeNode {res:=tfindIncreasingBST(root)return res.Right
}
func findIncreasingBST(root *TreeNode)  {if root.Left!=nil{findIncreasingBST(root.Left)}root.Left=nilt.Right=roott=t.Rightif root.Right!=nil{findIncreasingBST(root.Right)}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391293.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【一针见血】 JavaScript this

JavaScript this 指向一站式解决转载于:https://www.cnblogs.com/xueyejinghong/p/8403987.html

基于ssm框架和freemarker的商品销售系统

项目说明 1、项目文件结构 2、项目主要接口及其实现 &#xff08;1&#xff09;Index&#xff1a; 首页页面&#xff1a;展示商品功能&#xff0c;可登录或查看商品详细信息 &#xff08;2&#xff09;登录&#xff1a;/ApiLogin 3、dao层 数据持久化层&#xff0c;把商品和用户…

c++飞扬的小鸟游戏_通过建立一个飞扬的鸟游戏来学习从头开始

c飞扬的小鸟游戏Learn how to use Scratch 3.0 by building a flappy bird game in this course developed by Warfame. Scratch is a free programming language and online community where you can create your own interactive stories, games, and animations. Scratch is…

345

345 转载于:https://www.cnblogs.com/Forever77/p/11512701.html

简·雅各布斯指数第二部分:测试

In Part I, I took you through the data gathering and compilation required to rank Census tracts by the four features identified by Jane Jacobs as the foundation of a great neighborhood:在第一部分中 &#xff0c;我带您完成了根据简雅各布斯(Jacobs Jacobs)所确定…

Docker 入门(3)Docke的安装和基本配置

1. Docker Linux下的安装 1.1 Docker Engine 的版本 社区版 ( CE, Community Edition ) 社区版 ( Docker Engine CE ) 主要提供了 Docker 中的容器管理等基础功能&#xff0c;主要针对开发者和小型团队进行开发和试验企业版 ( EE, Enterprise Edition ) 企业版 ( Docker Engi…

python:单元测试框架pytest的一个简单例子

之前一般做自动化测试用的是unitest框架&#xff0c;发现pytest同样不错&#xff0c;写一个例子感受一下 test_sample.py import cx_Oracle import config from send_message import send_message from insert_cainiao_oracle import insert_cainiao_oracledef test_cainiao_mo…

mkdir命令使用范例

mkdir -p dir1/dir2/dir3/dir4 :-p 创建不存在的中间目录mkdir -m 000 demdir &#xff1a;-m 000 为新创建的目录指定权限转载于:https://blog.51cto.com/2685141/2068162

pwa 问题_您真的需要PWA吗? 这里有四个问题可以帮助您做出决定。

pwa 问题为什么需要PWA并不成问题。 让我们看看为什么您可能不需要它 (Why you need a PWA is not in question. Let’s see why you may NOT need it) My inbox has been filled with questions regarding PWAs after my last two articles. 在上两篇文章之后&#xff0c;我的…

利用ssh反向代理以及autossh实现从外网连接内网服务器

http://www.cnblogs.com/kwongtai/p/6903420.html转载于:https://www.cnblogs.com/littlehb/p/7598037.html

抑郁症损伤神经细胞吗_使用神经网络探索COVID-19与抑郁症之间的联系

抑郁症损伤神经细胞吗The drastic changes in our lifestyles coupled with restrictions, quarantines, and social distancing measures introduced to combat the corona virus outbreak have lead to an alarming rise in mental health issues all over the world. Social…

倦怠和枯燥_如何不断学习(不倦怠)

倦怠和枯燥In tech, constantly learning (both in and out of work) is an unstated job requirement. 在科技界&#xff0c;不断学习(工作中和工作中)是一项未阐明的工作要求。 When I was growing up, I would go to the bookstore with my dad every weekend, and every t…

Xcode 9.0 新增功能大全

Xcode是用于为Apple TV&#xff0c;Apple Watch&#xff0c;iPad&#xff0c;iPhone和Mac创建应用程序的完整开发人员工具集。Xcode开发环境采用tvOS SDK&#xff0c;watchOS SDK&#xff0c;iOS SDK和macOS SDK的形式捆绑Instruments分析工具&#xff0c;Simulator和OS框架。 …

Docker 入门(4)镜像与容器

1. 镜像与容器 1.1 镜像 Docker镜像类似于未运行的exe应用程序&#xff0c;或者停止运行的VM。当使用docker run命令基于镜像启动容器时&#xff0c;容器应用便能为外部提供服务。 镜像实际上就是这个用来为容器进程提供隔离后执行环境的文件系统。我们也称之为根文件系统&a…

python:pytest中的setup和teardown

原文&#xff1a;https://www.cnblogs.com/peiminer/p/9376352.html  之前我写的unittest的setup和teardown&#xff0c;还有setupClass和teardownClass&#xff08;需要配合classmethod装饰器一起使用&#xff09;&#xff0c;接下来就介绍pytest的类似于这类的固件。 &#…

如何开始使用任何类型的数据? - 第1部分

从数据开始 (START WITH DATA) My data science journey began with a student job in the Advanced Analytics department of one of the biggest automotive manufacturers in Germany. I was nave and still doing my masters.我的数据科学之旅从在德国最大的汽车制造商之一…

iHealth基于Docker的DevOps CI/CD实践

本文由1月31日晚iHealth运维技术负责人郭拓在Rancher官方技术交流群内所做分享的内容整理而成&#xff0c;分享了iHealth从最初的服务器端直接部署&#xff0c;到现在实现全自动CI/CD的实践经验。作者简介郭拓&#xff0c;北京爱和健康科技有限公司&#xff08;iHealth)。负责公…

从早期的初创企业到MongoDB的经理(播客)

In this weeks podcast episode, I chat with Harry Wolff, an engineering manager at MongoDB in New York City. Harry has been in the world of tech for over a decade, holding jobs in various startups before ending up at Mongo. 在本周的播客节目中&#xff0c;我与…

leetcode 1011. 在 D 天内送达包裹的能力(二分法)

传送带上的包裹必须在 D 天内从一个港口运送到另一个港口。 传送带上的第 i 个包裹的重量为 weights[i]。每一天&#xff0c;我们都会按给出重量的顺序往传送带上装载包裹。我们装载的重量不会超过船的最大运载重量。 返回能在 D 天内将传送带上的所有包裹送达的船的最低运载…

python:pytest优秀博客

上海悠悠&#xff1a;https://www.cnblogs.com/yoyoketang/tag/pytest/ 转载于:https://www.cnblogs.com/gcgc/p/11514345.html