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).给定一棵树,按中序遍历顺序重新排列树,以便树中最左边的节 ...
随机推荐
- Implementation:Dijkstra
#include <iostream> #include <cstdlib> #include <utility> #include <queue> u ...
- swiper h5学习
http://www.swiper.com.cn/ 较多用于移动端
- cookie封装函数与使用方法(转)
函数封装: var Cookie = function(name, value, options) { // 如果第二个参数存在 if (typeof value != 'undefined') { ...
- RESTful API的十个最佳实践
WebAPI在过去几年里非常的盛行,我们很多以往的技术手段都慢慢的转换为使用WebAPI来开发,因为它的语法简单规范化,以及轻量级等特点,这种方式收到了广泛的推崇. 通常我们使用RESTFul(Rep ...
- flutter Row里面元素居中显示
直接上代码: new Expanded( flex: , child: new Row( children: <Widget>[ Expanded( child: new Containe ...
- spring多线程初探
6月14号 晴 最高温度37 今天很热的一天啊,开发的任务现在正在测试阶段,手头没有什么工作任务,忙里偷闲,丰富一下我的blog. 前两天有个需求:调用第三方接口,这个接口的响应时间有点长,需 ...
- Redis 处理客户端连接的一些内部实现机制
本文主要介绍了 Redis 处理客户端连接的一些内部实现机制,包括连接处理.超时.缓冲区等一系列内容. 注:本文所述内容基于 Redis2.6 及以上版本. 连接的建立 Redis 通过监听一个 TC ...
- C/C++内存分区
C/C++编译的程序占用的内存分区 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数名,局部变量的名等.其操作方式类似于数据结构中的栈. 2.堆区(heap)— 由程序员分配释放, 若 ...
- [翻译] LLSimpleCamera
LLSimpleCamera https://github.com/omergul123/LLSimpleCamera LLSimpleCamera is a library for creating ...
- 如何生成.mobileprovision文件
如何生成.mobileprovision文件 本人视频教程系列 **.mobileprovision文件的生成的第一步就需要你提供一个用于开发的App ID 1. 创建App ID 创建一个bundl ...