897. Increasing Order Search Tree
题目来源:
https://leetcode.com/problems/increasing-order-search-tree/
自我感觉难度/真实难度:medium/easy
题意:
分析:
自己的代码:
优秀代码:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
def increasingBST(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
array=self.inOrder(root)
if not array:
return
newRoot=TreeNode(array[0])
curr=newRoot
for i in range(1,len(array)):
curr.right=TreeNode(array[i])
curr=curr.right
return newRoot def inOrder(self,root): if not root:
return []
res=[]
res.extend(self.inOrder(root.left))
res.append(root.val)
res.extend(self.inOrder(root.right))
return res
代码效率/结果:
Runtime: 200 ms, faster than 45.33% of Python3 online submissions forIncreasing Order Search Tree.
class Solution:
def increasingBST(self, root):
dummy = TreeNode(0)
self.prev = dummy
def inorder(root):
if not root: return None
inorder(root.left)
root.left = None
self.prev.right = root
self.prev = root
inorder(root.right)
inorder(root)
return dummy.right
Runtime: 160 ms, faster than 63.16% of Python3 online submissions forIncreasing Order Search Tree.
自己优化后的代码:
反思改进策略:
1.前序遍历不熟悉,需要熟练编写这个代码
2.看不懂优化的第二个解答: .prev TreeNode(0)
897. Increasing Order Search Tree的更多相关文章
- 【Leetcode_easy】897. Increasing Order Search Tree
problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完
- LeetCode 897 Increasing Order Search Tree 解题报告
题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...
- [LeetCode] 897. Increasing Order Search Tree 递增顺序查找树
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- [LeetCode&Python] Problem 897. Increasing Order Search Tree
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- 【leetcode】897. Increasing Order Search Tree
题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...
- 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...
- LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)
897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...
- [Swift]LeetCode897. 递增顺序查找树 | Increasing Order Search Tree
Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root o ...
- LeetCode.897-递增搜索树(Increasing Order Search Tree)
这是悦乐书的第346次更新,第370篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第211题(顺位题号是897).给定一棵树,按中序遍历顺序重新排列树,以便树中最左边的节 ...
随机推荐
- BZOJ3451:Tyvj1953 Normal
根据期望的线性性,答案就是 \(\sum\) 每个连通块出现次数的期望 而每个连通块次数的期望就是 \(\sum\) 连通块的根与每个点连通次数的期望 也就是对于一条路径 \((i,j)\),设 \( ...
- Matlab给三维点云添加高斯噪声和随机噪声
写在前面 在我们进行点云配准一类的模拟实验时,第一步就是对原始点云进行适当的RT变换,并添加一定的噪声,得到测量点云,然后才可以用我们的算法去进行后面的配准操作.在添加噪声这一块,matlab里并没有 ...
- input pattern中常用的正则表达式
常用的正则表达式 pattern的用法,只是列出来一些常用的正则: 信用卡 [0-9]{13,16} 银联卡 ^62[0-5]\d{13,16}$ Visa: ^4[0-9]{12}(?:[0-9]{ ...
- js dictionary字典 遍历
var dic={A:"AA",B:"BB",C:"CC"} //不能length去for循环(length:undefined) dic[ ...
- sqlserver年月日转汉字大写--自定义函数--繁体
两个自定义函数结合 函数一: create function convertNumToChinese ()) ) as begin ) ' set @temStr = '壹' ' set @temSt ...
- Excel2010取消隐藏的工作簿
背景 Excel 2010文件,其中包含针对业务需要涉及的计算器等,其中一个Worksheet用于存放计算器用到的常量,针对业务人员(即此Excel文件的用户)是隐藏的,并有密码保护. 现象 因业务变 ...
- 通过代码动态创建IIS站点
对WebApi进行单元测试时,一般需要一个IIS站点,一般的做法,是通过写一个批处理的bat脚本来实现,其实通过编码,也能实现该功能. 主要有关注三点:应用程序池.Web站点.绑定(协议类型:http ...
- C#秒转换小时
#region 秒转换小时 SecondToHour /// <summary> /// 秒转换小时 /// </summary> /// <param name=&qu ...
- .NET版UEditor报请求后台配置项http错误,上传功能无法使用的错误解决
在配置UEditor的时候,总是报请求后台配置项http错误,上传功能将不能正常使用!,上传图片等功能都无法使用.折磨了一下午,逐步调试发现了错误原因:
- 转 Ubuntu Linux 环境变量PATH设置
Ubuntu Linux系统环境变量配置文件: /etc/profile : 在登录时,操作系统定制用户环境时使用的第一个文件 ,此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行. ...