作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/increasing-order-search-tree/description/

题目描述

Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.

Example 1:

  1. Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]
  2. 5
  3. / \
  4. 3 6
  5. / \ \
  6. 2 4 8
  7. / / \
  8. 1 7 9
  9. Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
  10. 1
  11. \
  12. 2
  13. \
  14. 3
  15. \
  16. 4
  17. \
  18. 5
  19. \
  20. 6
  21. \
  22. 7
  23. \
  24. 8
  25. \
  26. 9

Note:

  1. The number of nodes in the given tree will be between 1 and 100.
  2. Each node will have a unique integer value from 0 to 1000.

题目大意

把一棵树按照中序遍历的顺序重新安排,安排成最左侧的节点是新的数树的根节点,并且每个节点只有右子节点。

解题方法

重建二叉树

好久没做树的题目,有点生疏。使用的方式是最简单的,先中序遍历,得到顺序,然后再连接的方式。

这个做法的问题是用数组保存了整儿个中序遍历的值,然后重建了二叉树,那么空间复杂度挺大的,不是一个好方法。

时间复杂度是O(n),空间复杂度是O(n).

代码如下:

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution(object):
  8. def increasingBST(self, root):
  9. """
  10. :type root: TreeNode
  11. :rtype: TreeNode
  12. """
  13. array = self.inOrder(root)
  14. if not array:
  15. return None
  16. newRoot = TreeNode(array[0])
  17. curr = newRoot
  18. for i in range(1, len(array)):
  19. curr.right =TreeNode(array[i])
  20. curr = curr.right
  21. return newRoot
  22. def inOrder(self, root):
  23. if not root:
  24. return []
  25. array = []
  26. array.extend(self.inOrder(root.left))
  27. array.append(root.val)
  28. array.extend(self.inOrder(root.right))
  29. return array

数组保存节点

在上面解法的基础上,如果不想使用保存节点的值然后重新构建每个节点的方式,那么有个更简单的方法就是我们在数组里保存节点,然后直接把数组的节点再次构成树就好了。省去了重新构造每个节点的过程。

时间复杂度是O(n),空间复杂度是O(n).

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution(object):
  8. def increasingBST(self, root):
  9. """
  10. :type root: TreeNode
  11. :rtype: TreeNode
  12. """
  13. res = self.inOrder(root)
  14. if not res:
  15. return
  16. dummy = TreeNode(-1)
  17. cur = dummy
  18. for node in res:
  19. node.left = node.right = None
  20. cur.right = node
  21. cur = cur.right
  22. return dummy.right
  23. def inOrder(self, root):
  24. if not root:
  25. return []
  26. res = []
  27. res.extend(self.inOrder(root.left))
  28. res.append(root)
  29. res.extend(self.inOrder(root.right))
  30. return res

中序遍历时修改指针

这个做法在上面的基础上再次缩减了空间复杂度,不再需要数组。这种做法中直接在中序遍历的过程中修改每个节点的指向。

修改指向的方式其实比较简单,使用prev指针一直指向了构造出来的这个新树的最右下边的节点,在中序遍历过程中把当前节点的左指针给设置为None,然后把当前节点放到新树的右下角,这样类似于一个越来越长的链表的构建过程。

时间复杂度是O(n),空间复杂度是O(1).

  1. # Definition for a binary tree node.
  2. # class TreeNode(object):
  3. # def __init__(self, x):
  4. # self.val = x
  5. # self.left = None
  6. # self.right = None
  7. class Solution(object):
  8. def increasingBST(self, root):
  9. """
  10. :type root: TreeNode
  11. :rtype: TreeNode
  12. """
  13. dummy = TreeNode(-1)
  14. self.prev = dummy
  15. self.inOrder(root)
  16. return dummy.right
  17. def inOrder(self, root):
  18. if not root:
  19. return None
  20. self.inOrder(root.left)
  21. root.left = None
  22. self.prev.right = root
  23. self.prev = self.prev.right
  24. self.inOrder(root.right)

参考资料

https://zxi.mytechroad.com/blog/tree/leetcode-897-increasing-order-search-tree/

日期

2018 年 9 月 3 日 ———— 新学期开学第一天!
2018 年 11 月 1 日 —— 小光棍节

【LeetCode】897. Increasing Order Search Tree 解题报告(Python)的更多相关文章

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

  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 root o ...

  3. 【Leetcode_easy】897. Increasing Order Search Tree

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

  4. 897. Increasing Order Search Tree

    题目来源: https://leetcode.com/problems/increasing-order-search-tree/ 自我感觉难度/真实难度:medium/easy 题意: 分析: 自己 ...

  5. 【leetcode】897. Increasing Order Search Tree

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

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

  7. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

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

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

  9. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

随机推荐

  1. LearnPython_week4

    1.装饰器2.生成器3.迭代器4.内置方法5.可序列化6.项目规范化 1.装饰器 # -*- coding:utf-8 -*- # Author:Wong Du ### 原代码 def home(): ...

  2. Mybatis逆向工程简单介绍

    转自:https://blog.csdn.net/yerenyuan_pku/article/details/71909325 什么是逆向工程 MyBatis的一个主要的特点就是需要程序员自己编写sq ...

  3. promise.all的应用场景举例

    Promise.all方法 简而言之:Promise.all( ).then( )适用于处理多个异步任务,且所有的异步任务都得到结果时的情况. 比如:用户点击按钮,会弹出一个弹出对话框,对话框中有两部 ...

  4. JS模块化,Javascript 模块化管理的历史

    模块管理这个概念其实在前几年前端度过了刀耕火种年代之后就一直被提起. 直接回想起来的就是 cmd amd commonJS 这三大模块管理的印象.接下来,我们来详细聊聊. 一.什么是模块化开发 为了让 ...

  5. FileReader (三) - 网页拖拽并预显示图片简单实现

    以下是一个很贱很简单的一个 在网页上图拽图片并预显示的demo. 我是从https://developer.mozilla.org/en-US/docs/Web/API/FileReader#Stat ...

  6. 【leetcode】563. Binary Tree Tilt

    Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node i ...

  7. 【二分答案】CF1613 C. Poisoned Dagger

    题目:Problem - C - Codeforces 本题的优解是二分答案,但我其实不会二分,本质是用了两个指针作为边界,然后不断对半缩小范围来快速确定答案. 神奇的二分法 代码: #include ...

  8. ALitum技巧

    创建异型焊盘的方法 SCH与PCB同步修改后元器件乱跑的解决方法 Altium 在PCB重新编号更新到SCH原理图的方法 同步问题 其他技巧: 当前层亮色,其他层灰色切换:SHIFT+S

  9. c++string转const char*与char*

    #include <iostream> #include <string> #include <memory> using namespace std; const ...

  10. Can we use function on left side of an expression in C and C++?

    In C, it might not be possible to have function names on left side of an expression, but it's possib ...