题目来源:
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的更多相关文章

  1. 【Leetcode_easy】897. Increasing Order Search Tree

    problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完

  2. 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 ...

  3. [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 ...

  4. [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 ...

  5. 【leetcode】897. Increasing Order Search Tree

    题目如下: 解题思路:我的方法是先用递归的方法找出最左边的节点,接下来再对树做一次递归中序遍历,找到最左边节点后将其设为root,其余节点依次插入即可. 代码如下: # Definition for ...

  6. 【LeetCode】897. Increasing Order Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 重建二叉树 数组保存节点 中序遍历时修改指针 参考资 ...

  7. LeetCode 897. 递增顺序查找树(Increasing Order Search Tree)

    897. 递增顺序查找树 897. Increasing Order Search Tree 题目描述 给定一个树,按中序遍历重新排列树,使树中最左边的结点现在是树的根,并且每个结点没有左子结点,只有 ...

  8. [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 ...

  9. LeetCode.897-递增搜索树(Increasing Order Search Tree)

    这是悦乐书的第346次更新,第370篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第211题(顺位题号是897).给定一棵树,按中序遍历顺序重新排列树,以便树中最左边的节 ...

随机推荐

  1. python学习之老男孩python全栈第九期_数据库day001 -- 作业

    创建如图所示数据库: 创建过程:  查看数据库,创建数据库 db1,再查看一下数据库  进入数据库,查看一下表  接着再创建一个class表 发现增加了重复数据,因此要把第二个修改一下  修改完数据之 ...

  2. 第二十天- 多继承 经典MRO 新式MRO super()

    # 多继承:# 在继承关系中.⼦类自动拥有⽗类中除私有属性外其他所有内容.python⽀持多继承.子类可拥有多⽗类. class ShenXian: # 神仙 def fei(self): print ...

  3. vscode 代码跳转之PHP篇

    1.安装插件:PHP IntelliSense 2.配置:"php.executablePath": "C:\\php\\php.exe", 但是目前有问题,跨 ...

  4. 当碰到需要调试打包后的js

    在react中经常开发碰到不能热更中进行调试的,如IE之类的 这个时候我们就需要打包才能运行看到效果, 但是往往每次打包都需要很长的时间: 这个时候我们就可以直接找到打包后的文件,直接在改文件中修改: ...

  5. 安装php扩展redis (windows环境)

    首先十分感谢网络上支持开源分享的前辈们,资源真的太丰富了,虽然也有许多优秀的国外资源被墙了... 想要给php增加redis扩展第一步当然要知道自己使用的php版本以及一些配置.查看 phpinfo ...

  6. Web 开发者学习路线图

      2017 Web 开发者学习路线图(头图源自谷歌) 本文是源自 Github 上 Kamran Ahmed 建立的一个仓库.在文中,作者为他的老教授分享了一组成为前端与后端开发者以及 Devops ...

  7. 【c++错误】类的语法错误 error c2533:constructors not allowed a return type(构造函数不允许返回一个类型)

    今天编写类的程序的时候不小心把类后的分号忘写了,就出现上面的错误提示. 顺便复习下类的正确格式: class 类名 { public: //习惯上将公有类型放在前面,便于阅读 ……(外部接口) pro ...

  8. js中innerHTML和innerText的用法

    <div id="test"> <span style="color:red">test1</span> test2 < ...

  9. opencv3.2.0实现视频抽帧,并保存成图片

    .实现指定帧数的抽取.和全部帧数的抽取,并保存到指定目录. 在QT新建一个控制台程序,程序源码如下:(程序实现每十帧获取一次帧) #include <QCoreApplication> # ...

  10. Pwn with File结构体(二)

    前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 最新版的 libc 中会对 vtable 检查,所以之前的攻击方式 ...