1. class Solution {
  2. public:
  3. void flatten(TreeNode* root) {
  4. while(root){
  5. if(root->left){
  6. TreeNode* pre=root->left;
  7. while(pre->right){
  8. pre=pre->right;
  9.  
  10. }
  11. pre->right=root->right;
  12. root->right=root->left;
  13. root->left=nullptr;
  14. }
  15. root=root->right;
  16. }
  17. }
  18. };

补充一个DFS遍历,然后倒叙组装成类似链表的解决方案,使用python实现:

  1. class Solution:
  2. def preOrder(self,root,l):
  3. if root != None:
  4. l.append(root)
  5.  
  6. self.preOrder(root.left,l)
  7. self.preOrder(root.right,l)
  8.  
  9. def flatten(self, root: 'TreeNode') -> 'None':
  10. if root == None:
  11. return
  12. l = list()
  13. self.preOrder(root,l)
  14. root = l[len(l)-1]
  15. for i in range(len(l)-2,-1,-1):
  16. l[i].left = None
  17. l[i].right = root
  18. root = l[i]

python的递归实现:

  1. class Solution:
  2. def build(self,root):
  3. if root != None:
  4. if root.left != None and root.right != None:
  5. right = self.build(root.right)#重建右子树
  6. left = self.build(root.left)#重建左子树
  7. leaf = left
  8. while leaf.right != None:#找到左子树的叶子节点
  9. leaf = leaf.right
  10. leaf.right = right#右子树连接到左子树的末尾
  11. root.right = left#根节点修改右子树
  12. root.left = None#根结点左子树设为空
  13. return root
  14. elif root.left != None and root.right == None:
  15. root.right = self.build(root.left)
  16. root.left = None
  17. return root
  18. elif root.left == None and root.right != None:
  19. root.right = self.build(root.right)
  20. return root
  21. else:
  22. return root
  23. return None
  24.  
  25. def flatten(self, root: TreeNode) -> None:
  26. """
  27. Do not return anything, modify root in-place instead.
  28. """
  29. self.build(root)

leetcode114的更多相关文章

  1. [Swift]LeetCode114. 二叉树展开为链表 | Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  2. Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表

    给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 class Solution { publ ...

  3. LeetCode114 Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. (Medium) For example,Given 1 / \ 2 5 / \ ...

  4. leetcode114:valid-sudoku

    题目描述 根据数独的规则Sudoku Puzzles - The Rules.判断给出的局面是不是一个符合规则的数独局面 数独盘面可以被部分填写,空的位置用字符'.'.表示 这是一个部分填写的符合规则 ...

随机推荐

  1. guava-retrying 源码解析(等待策略详解)

    一.等待策略相关类: 1.等待策略接口:WaitStrategy接口 该接口只有一个方法,就是返回尝试失败之后,下一次尝试之前的等待时间.long computeSleepTime(Attempt f ...

  2. 剑指Offer 19. 顺时针打印矩阵 (其他)

    题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数 ...

  3. 强大的拖拽组件:React DnD 的使用

    强大的拖拽组件:React DnD 的使用 react.js 10.6k 次阅读  ·  读完需要 25 分钟 17 文章首发我的个人blog : 原文链接 学习 React DnD 的最初原因是阅读 ...

  4. GoogLeNet 之 Inception-v1 解读

    本篇博客的目的是展示 GoogLeNet 的 Inception-v1 中的结构,顺便温习里面涉及的思想. Going Deeper with Convolutions:http://arxiv.or ...

  5. SQLI DUMB SERIES-12

    (1)检测闭合方式:在username上输入" admin" " 说明输入的username后还有双引号和括号 方法一: (2)通过其他途径知道用户名即可.如 输入&qu ...

  6. 学习笔记TF036:实现Bidirectional LSTM Classifier

    双向循环神经网络(Bidirectional Recurrent Neural Networks,Bi-RNN),Schuster.Paliwal,1997年首次提出,和LSTM同年.Bi-RNN,增 ...

  7. mongodb分片balance

    查看balance状态 mongos> sh.getBalancerState()true   通过balance锁查看balance活动 如果state是2,表示balance锁已经被获取 m ...

  8. MySQL Error--打开过多文件导致数据库无法连接

    [此文抄自同事的邮件,当作笔记学习] 环境描述Mysql 5.5.21OS centos 5.8zabbix agent 2.4.3 情况描述现象数据库处于运行状态,但是无法创建新的连接,监控报警数据 ...

  9. kafka-producer partitioner.class的使用

    partitioner.class的说明   在API客户端中封装好的partition( )方法会为消息选择一个分区编号.为了保证消息负载均衡到每个分区,可以通过使用默认方式或者 手动配置这个参数的 ...

  10. MySQL Point in Time Recovery the Right Way

    In this blog, I’ll look at how to do MySQL point in time recovery (PITR) correctly. Sometimes we nee ...