[LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展开成链表
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order trave
这道题要求把二叉树展开成链表,根据展开后形成的链表的顺序分析出是使用先序遍历,那么只要是数的遍历就有递归和非递归的两种方法来求解,这里我们也用两种方法来求解。首先来看递归版本的,思路是先利用 DFS 的思路找到最左子节点,然后回到其父节点,把其父节点和右子节点断开,将原左子结点连上父节点的右子节点上,然后再把原右子节点连到新右子节点的右子节点上,然后再回到上一父节点做相同操作。代码如下:
解法一:
class Solution {
public:
void flatten(TreeNode *root) {
if (!root) return;
if (root->left) flatten(root->left);
if (root->right) flatten(root->right);
TreeNode *tmp = root->right;
root->right = root->left;
root->left = NULL;
while (root->right) root = root->right;
root->right = tmp;
}
};
例如,对于下面的二叉树,上述算法的变换的过程如下:
/ \
/ \ \
/ \
\ \
3
\
\
2
\
3
\
4
\
\
下面再来看非迭代版本的实现,这个方法是从根节点开始出发,先检测其左子结点是否存在,如存在则将根节点和其右子节点断开,将左子结点及其后面所有结构一起连到原右子节点的位置,把原右子节点连到元左子结点最后面的右子节点之后。代码如下:
解法二:
class Solution {
public:
void flatten(TreeNode *root) {
TreeNode *cur = root;
while (cur) {
if (cur->left) {
TreeNode *p = cur->left;
while (p->right) p = p->right;
p->right = cur->right;
cur->right = cur->left;
cur->left = NULL;
}
cur = cur->right;
}
}
};
例如,对于下面的二叉树,上述算法的变换的过程如下:
/ \
/ \ \
\
2
/ \
3 4
\
\
\
\
\
\
\
前序迭代解法如下:
解法三:
class Solution {
public:
void flatten(TreeNode* root) {
if (!root) return;
stack<TreeNode*> s;
s.push(root);
while (!s.empty()) {
TreeNode *t = s.top(); s.pop();
if (t->left) {
TreeNode *r = t->left;
while (r->right) r = r->right;
r->right = t->right;
t->right = t->left;
t->left = NULL;
}
if (t->right) s.push(t->right);
}
}
};
此题还可以延伸到用中序,后序,层序的遍历顺序来展开原二叉树,分别又有其对应的递归和非递归的方法,有兴趣的童鞋可以自行实现。
Github 同步地址:
https://github.com/grandyang/leetcode/issues/114
类似题目:
Flatten a Multilevel Doubly Linked List
参考资料:
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展开成链表的更多相关文章
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
- [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- 114. Flatten Binary Tree to Linked List -- 将二叉树转成链表(in-place单枝树)
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [LeetCode] 114. 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 ...
- [leetcode]114. 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 ...
- [leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表
/* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten( ...
- leetcode 114 Flatten Binary Tree to Linked List ----- java
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
随机推荐
- Vue全选和全不选
HTML代码: <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> ...
- C# 重载 overload,重写override覆盖
重载overload 派生类和基类的方法同名,其中访问修饰符(public,private,protected,internal,protected internal),返回类型,参数类型,个数,顺序 ...
- Oracle - 数字处理 - 取上取整、向下取整、保留N位小数、四舍五入、数字格式化
用oracle sql对数字进行操作: 取上取整.向下取整.保留N位小数.四舍五入.数字格式化 取整(向下取整): select floor(5.534) from dual; select trun ...
- word转html预览
#region Index页面 /// <summary> /// Index页面 /// </summary> /// <paramname="url&quo ...
- Python struct与小端存储
参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017685387246080 在使用Python 实现字符向字节数据类型转换的时候,P ...
- apache-tomcat-7.0.94在Windows上启动时,控制台黑窗口出现乱码解决
一.问题 二.解决 原因是tomcat日志编码的配置问题. 打开tomcat/conf/logging.properties配置文件. 把编码注释掉或者改为gbk就可以了. 参考:https://bl ...
- 斗鱼刷弹幕js代码
对于一个网络喷子(like me)来说,喷人必须高效. var script=document.createElement("script"); script.type=" ...
- 绕过基于签名的XSS筛选器:修改HTML
绕过基于签名的XSS筛选器:修改HTML 在很多情况下,您可能会发现基于签名的过滤器只需切换到一个不太熟悉的执行脚本的方法即可.如果失败了,您需要查看混淆攻击的方法. 本文提供了HTML语法可以被混淆 ...
- JDK1.8 —— 接口定义增强
使用default和static定义接口方法 JDK1.8(jre8)以后,接口中不在仅仅只允许定义抽象方法,开始允许定义普通方法了:而普通方法需要用default声明. interface IMes ...
- rem与em的使用和区别详解【转】
目录 最大的问题是 主要区别 rem 单位如何转换为像素值 em 单位如何转换为像素值 Em 单位的遗传效果 Em 继承的例子 浏览器设置 HTML 元素字体大小的影响 没有设置 HTML 字体大 ...