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).给定一棵树,按中序遍历顺序重新排列树,以便树中最左边的节 ...
随机推荐
- .NET中的异步编程
开篇 异步编程是程序设计的重点也是难点,还记得在刚开始接触.net的时候,看的是一本c#的Winform实例教程,上面大部分都是教我们如何使用Winform的控件以及操作数据库的实例,那时候做的基本都 ...
- Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) SyntaxError: Unexpected token R in JSON at position 0 at JSON.parse (<anonymous>)
这个问题在之前做项目时碰到过一次,当时按照网上的做法,去掉JSON.parse()这一层转换后就没有这个报错了,数据也能正常使用,就没多想,也没深究是什么原因.可是这次又碰到了,所以这次我必须要弄明白 ...
- HiveSql调优经验
背景 在刚使用hive的过程中,碰到过很多问题,任务经常需要运行7,8个小时甚至更久,在此记录一下这个过程中,我的一些收获 join长尾 背景 SQL在Join执行阶段会将Join Key相同的数据分 ...
- flutter控件之CheckBox
import 'package:flutter/material.dart'; class LearnCheckBox extends StatefulWidget{ @override State& ...
- Pig的使用场景
数据转换加载(ETL)数据流:读取原始数据(比如用户日志),进行数据清洗,进行简单的预计算后导入到数据仓库,比如join连接数据库里的用户信息.
- MongoDB用户配置
MongoDB学习笔记—权限管理 阅读目录 1.MongoDB权限介绍 2 MongoDB添加管理员账户 3 MongoDB开启用户权限验证 4 MongoDB的roles角色简单介绍 5 Mongo ...
- VisualSVN Server迁移的方法
VisualSVN Server迁移涉及到两种情况: 第一种情况:VisualSVN Server没有更换电脑或者服务器,只是修改Server name. 第二种情况:当VisualSVN Serve ...
- java多线程读取、操作List集合
import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.ArrayUtils; pub ...
- sql面试
1.用一条SQL语句 查询出每门课都大于80分的学生姓名 name kecheng fenshu 张三 语文 81张三 数学 75李四 语文 ...
- php5 Array 数组函数
函数 描述 array() 创建数组. array_change_key_case() 把数组中所有键更改为小写或大写. array_chunk() 把一个数组分割为新的数组块. array_colu ...