114. Flatten Binary Tree to Linked List (Stack, Tree; DFS)
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
法I:递归,前序遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode* root) {
if(!root) return;
else preOrderTraverse(root);
} TreeNode* preOrderTraverse(TreeNode* root){
TreeNode* rightHead = root->right; //save root->right
TreeNode* tail; if(!root->left && !rightHead){
return root;
}
if(root->left) { //have left child
root->right = root->left;
root->left = NULL;
tail = preOrderTraverse(root->right);
if(rightHead){ //have both left child and right child
tail->right = rightHead; //如果left==NULL, 这里就不能使用tail->right,所以得分开讨论有右子树情况
tail = preOrderTraverse(rightHead);
}
}
else{ //only have right child
tail = preOrderTraverse(rightHead);
} return tail;
}
};
法II:迭代
每次循环,找到左子树前序遍历的最后一个节点(即最右的叶子节点),把右节点作为它的右儿子,当前节点的右儿子置为左节点,左节点置为NULL。
然后把当前节点挪到它的右儿子,进入下一次循环
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode *root) {
if (root == NULL) return;
TreeNode *cur = root, *tail = NULL;
while (cur != NULL) {
if (cur->left != NULL) {
tail = cur->left;
while (tail->right) tail = tail->right;
tail->right = cur->right;
cur->right = cur->left;
cur->left = NULL;
}
cur = cur->right;
}
}
};
114. Flatten Binary Tree to Linked List (Stack, Tree; DFS)的更多相关文章
- 114 Flatten Binary Tree to Linked List [Python]
114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二 ...
- [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 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 ...
- 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
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- [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 ...
- [LC] 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 OJ 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 T ...
- [LeetCode]题解(python):114 Flatten Binary Tree to Linked List
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...
随机推荐
- Linux 错误码对照表
errno 在 <errno.h> 中定义,错误 Exx 的宏定义在 /usr/include/asm-generic 文件夹下面的 errno-base.h 和 errno.h,分别定 ...
- ecmall类关系图(转)
- 分享百度文件上传组件webUploader的使用demo
先创建DOM节点:<head ng-app="myApp"> <meta charset="UTF-8"> <title>& ...
- 初学HTML之HTML介绍
众所周知现在的H5.大数据.云计算都是热门的.其实想学好一门语言重点是多看多想多写多练. 我在博客中会从基础开始讲解HTML4.0.中间加入HTML5的新标签 在这先给大家推荐几个开发工具: note ...
- C#(.Net)中调用Sql sever汉字字符串显示为?问号
利用Sql语言,向数据库中导入‘C语’,结果在检查的时候,发现如上图所示. 网络上,很多人说是编码问题,但是都没给出具体的解决方案,最终用这种方法解决了! 把上图中需要储存汉字字符串的类型变为 nva ...
- TMS320CC657基本外围电路调试
一.本文内容 本文主要包含以下三个基本外围电路的调试过程与调试结果: 电源模块 时钟模块 复位模块 二.电源模块调试 无论对FPGA还是DSP而言,对电源的上电顺序都有一定的要求,且不同型号的器件对电 ...
- java代码---实现随机产生1000个随机数,并10个一行的输出
总结:不会用,就是不熟 package com.s.x; //输入10个随机数,并显示最大值,最小值 import java.util.*; public class Value { public s ...
- 杂项-数学软件:Maple
ylbtech-杂项-数学软件:Maple Maple是目前世界上最为通用的数学和工程计算软件之一,在数学和科学领域享有盛誉,有“数学家的软件”之称.Maple 在全球拥有数百万用户,被广泛地应用于科 ...
- Go - reflection
Go 语言也有反射的机制,通过这种机制可以大大提高程序的灵活性. reflect包中的 TypeOf 与 ValueOf 方法 首先,当我们想要使用反射技术的时候,我们需要先引用 reflect 包. ...
- request和response的复习
客户端发来的请求,服务器将请求封装成request对象,包括请求头和请求的数据等.创建response对象,调用Servlet的Service()方法传递这两个参数,使用HttpServlet就是将这 ...