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.判断给出的局面是不是一个符合规则的数独局面 数独盘面可以被部分填写,空的位置用字符'.'.表示 这是一个部分填写的符合规则 ...
随机推荐
- web 页面上纯js实现按钮倒计数功能(实时计时器也可以)
需求构思:本功能想实现的是,一个按钮在页面载入就显示提醒续费,,,倒数60秒后,完成提醒功能,可以按另外一个页面跳转到主页. 参考网上的大神,实现如下:Button2倒数,Button3跳转,在页面上 ...
- Tomcat version 7.0 only supports J2EE 1.2, 1.3, 1.4, and Java EE 5 Web modules
原文出处:http://jingwang0523.blog.163.com/blog/static/9090710320113294551497/ 最近在用eclipse做项目,新建项目时什么都贪新, ...
- 通过pid杀死进程
bool ****::KillProcess(DWORD pid) { // When the all operation fail this function terminate the " ...
- c#抓屏功能在DPI缩放后,截到的图片不完整的问题
/// <summary> /// 获取屏幕快照 /// </summary> /// <returns></returns> public stati ...
- Tomcat7.0/8.0 详细安装配置图解,以及UTF-8编码配置
Tomcat7.0/8.0 详细安装配置图解,以及UTF-8编码配置 2017年01月24日 10:01:48 阅读数:51265 标签: tomcattomcat安装tomcat配置tomcat编码 ...
- 10.python面向对象进阶功能
isinstance(obj,cls)和issubclass(sub,super)(1)isinstance(obj,cls)检查一个对象obj是否是一个类cls的实例(2)issubclass(su ...
- EasyUI datagrid 序列 化后时间 处理 九
@{ ViewBag.Title = "Home Page"; Layout = null; } <!DOCTYPE html> <html> <he ...
- 编译安装和apt安装Nginx1.14.0
安装依赖 yum -y install gcc gcc-c++yum -y install zlib zlib-devel openssl openssl-devel pcre-devel 在Ubun ...
- activiti学习第一天
公司项目组在考虑工作流,首选了activiti,首先我们要明确为什么要使用activiti,有什么好处. 在工作中有些项目会用到工作流,如果简单的项目,我们就无需使用类似activiti.jbpm等工 ...
- 互联网媒体类型 MIME Type
参考:https://zh.wikipedia.org/wiki/%E4%BA%92%E8%81%94%E7%BD%91%E5%AA%92%E4%BD%93%E7%B1%BB%E5%9E%8B 互联网 ...