题目来源:
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. YII使用beanstalk队列

    转载于:http://blog.csdn.net/yao970953039/article/details/41821387 1.系统centos 我是直接使用yum install beanstal ...

  2. AngularJS之过滤器

    AnularJS的过滤器用来格式化需要展示给用户的数据,有很多实用的内置过滤器,也可以自己编写. 在HTML中的模板绑定符号{{ }}内通过|符号来调用过滤器.例如,假设我们希望将字符串转换成大写,可 ...

  3. vue项目中引入bootstrap

    (1)引入Jquery文件,需要在bootstrap.min.js 之前引入. 1.npm install jquery --save-dev 2. plugins: [ new webpack.Pr ...

  4. scss-!optional

    optional翻译成汉语具有"可选的"的意思.顾名思义,!optional标记前面的扩展不必须生成一个新的选择器.看一段SCSS代码片段: p{ color:red; @exte ...

  5. Vue 页面15分钟无操作时返回首页

    这种需求手机端和pc端一般是不存在的,因为都是可以手动操作刷新的. 最近在做一个户外社区大屏的项目,因为大屏是全屏显示,没法手动刷新,不可能在页面专门做一个刷新按钮,也不好看,那这样的需求就显得格外重 ...

  6. <Android 基础(二十九)> Fragment (2) ~ DialogFragment

    简介 上一篇简单的介绍了下Fragment的使用方法,这一篇主要看下DialogFragment. 在android 3.0时被引入.是一种特殊的Fragment,用于在Activity的内容之上展示 ...

  7. 获取本地IP地址的vc代码

    作者:朱金灿 来源:http://blog.csdn.net/clever101 获取本地IP地址有两种做法.一种是使用gethostname函数,代码如下: bool CSocketComm::Ge ...

  8. Ubunt 安装mysql

    apt-get install mysql-client-core-5.6apt-get install mysql-client-5.6apt-get install mysql-server-5. ...

  9. SqlServer触发器实现表的级联插入、级联更新

    首先建立两张表,分别为test1与test2,期望在更改test1的时候,test2的相关记录能够同时做出更改.假定test1与test2的表结构相同,如下表所示 name age     触发器实现 ...

  10. Celery学习--- Celery在项目中的使用

    可以把celery配置成一个应用,注意连接文件命名必须为celery.py 目录格式如下 项目前提: 安装并启动Redis CeleryPro/celery.py   [命名必须为celery.py] ...