当前位置:网站首页>897. Increasing Order Search Tree
897. Increasing Order Search Tree
2022-08-09 07:57:00 【scwMason】
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.
Example 1: Input: [5,3,6,2,4,null,8,1,null,null,null,7,9] 5 / \ 3 6 / \ \ 2 4 8 / / \ 1 7 9 Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9] 1 \ 2 \ 3 \ 4 \ 5 \ 6 \ 7 \ 8 \ 9
Note:
- The number of nodes in the given tree will be between 1 and 100.
- Each node will have a unique integer value from 0 to 1000.
意思就是把就是按照先序遍历重新构成一个只有右子树的树
思路:
先创建一个空的头节点node,再定义一个指向node的tree(当前节点)然后递归遍历,直到碰到递归边界,就return,然后把当前节点放到tree的右子树上,并让tree重新指向自己的右子树,这样一层一层下来就行了
代码
class Solution:
tree=TreeNode(None)#头指针
node=tree#当前节点
def increasingBST(self, root: TreeNode) -> TreeNode:
if not root:#如果到递归边界
return
self.increasingBST(root.left)#遍历左子树
self.node.right=TreeNode(root.val)#创建节点放到当前
self.node=self.node.right#移动当前节点
self.increasingBST(root.right)#向右递归
return self.tree.right
边栏推荐
- CoCube传感器MPU6050笔记
- (四)BP神经网络预测(上)
- The String class objects created by the JVM memory allocation and the difference between equals and = =
- EXCEL使用函数联调(find,mid,vlookup,xlookup)
- SiamFC:用于目标跟踪的全卷积孪生网络 fully-convolutional siamese networks for object tracking
- 梅科尔工作室--BP神经网络培训笔记
- Set集合
- 一站制造项目及Spark核心面试 ,220808,,,
- oracle存储过程问题解答
- 3.MySQL插入数据, 读取数据、Where子句和Order By关键字
猜你喜欢
随机推荐
c语言位段
Redis(八)集群
db2数据库备份恢复问题
SQL存储过程
[STL]string
libtorch示例
oracle存储过程问题解答
Flexible and easy-to-use sql monitoring script part7
Forest Program DFS + tanjar cactus
LeetCode·每日一题·761.特殊的二进制序列·分治
图像处理(一)图像基础
(二)、灰色预测模型(GM1,1)
yolov5 detects the number of labels in the dataset
配置本地yum源仓库
研发分享:机器学习卡片的使用
低成本、大容量、高交互…Polkadot 引领 GameFi 实现新突破
(error) NOAUTH Authentication required.
静态路由的原理与配置
pragma comment的使用(转载)重新排版
.net(一)WebService创建