leetcode114
- class Solution {
- public:
- void flatten(TreeNode* root) {
- while(root){
- if(root->left){
- TreeNode* pre=root->left;
- while(pre->right){
- pre=pre->right;
- }
- pre->right=root->right;
- root->right=root->left;
- root->left=nullptr;
- }
- root=root->right;
- }
- }
- };
补充一个DFS遍历,然后倒叙组装成类似链表的解决方案,使用python实现:
- class Solution:
- def preOrder(self,root,l):
- if root != None:
- l.append(root)
- self.preOrder(root.left,l)
- self.preOrder(root.right,l)
- def flatten(self, root: 'TreeNode') -> 'None':
- if root == None:
- return
- l = list()
- self.preOrder(root,l)
- root = l[len(l)-1]
- for i in range(len(l)-2,-1,-1):
- l[i].left = None
- l[i].right = root
- root = l[i]
python的递归实现:
- class Solution:
- def build(self,root):
- if root != None:
- if root.left != None and root.right != None:
- right = self.build(root.right)#重建右子树
- left = self.build(root.left)#重建左子树
- leaf = left
- while leaf.right != None:#找到左子树的叶子节点
- leaf = leaf.right
- leaf.right = right#右子树连接到左子树的末尾
- root.right = left#根节点修改右子树
- root.left = None#根结点左子树设为空
- return root
- elif root.left != None and root.right == None:
- root.right = self.build(root.left)
- root.left = None
- return root
- elif root.left == None and root.right != None:
- root.right = self.build(root.right)
- return root
- else:
- return root
- return None
- def flatten(self, root: TreeNode) -> None:
- """
- Do not return anything, modify root in-place instead.
- """
- self.build(root)
leetcode114的更多相关文章
- [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 ...
- Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表
给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 class Solution { publ ...
- 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 / \ ...
- leetcode114:valid-sudoku
题目描述 根据数独的规则Sudoku Puzzles - The Rules.判断给出的局面是不是一个符合规则的数独局面 数独盘面可以被部分填写,空的位置用字符'.'.表示 这是一个部分填写的符合规则 ...
随机推荐
- guava-retrying 源码解析(等待策略详解)
一.等待策略相关类: 1.等待策略接口:WaitStrategy接口 该接口只有一个方法,就是返回尝试失败之后,下一次尝试之前的等待时间.long computeSleepTime(Attempt f ...
- 剑指Offer 19. 顺时针打印矩阵 (其他)
题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数 ...
- 强大的拖拽组件:React DnD 的使用
强大的拖拽组件:React DnD 的使用 react.js 10.6k 次阅读 · 读完需要 25 分钟 17 文章首发我的个人blog : 原文链接 学习 React DnD 的最初原因是阅读 ...
- GoogLeNet 之 Inception-v1 解读
本篇博客的目的是展示 GoogLeNet 的 Inception-v1 中的结构,顺便温习里面涉及的思想. Going Deeper with Convolutions:http://arxiv.or ...
- SQLI DUMB SERIES-12
(1)检测闭合方式:在username上输入" admin" " 说明输入的username后还有双引号和括号 方法一: (2)通过其他途径知道用户名即可.如 输入&qu ...
- 学习笔记TF036:实现Bidirectional LSTM Classifier
双向循环神经网络(Bidirectional Recurrent Neural Networks,Bi-RNN),Schuster.Paliwal,1997年首次提出,和LSTM同年.Bi-RNN,增 ...
- mongodb分片balance
查看balance状态 mongos> sh.getBalancerState()true 通过balance锁查看balance活动 如果state是2,表示balance锁已经被获取 m ...
- MySQL Error--打开过多文件导致数据库无法连接
[此文抄自同事的邮件,当作笔记学习] 环境描述Mysql 5.5.21OS centos 5.8zabbix agent 2.4.3 情况描述现象数据库处于运行状态,但是无法创建新的连接,监控报警数据 ...
- kafka-producer partitioner.class的使用
partitioner.class的说明 在API客户端中封装好的partition( )方法会为消息选择一个分区编号.为了保证消息负载均衡到每个分区,可以通过使用默认方式或者 手动配置这个参数的 ...
- 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 ...