【一天一道LeetCode】#114. Flatten Binary Tree to Linked List
一天一道LeetCode
本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github
欢迎大家关注我的新浪微博,我的新浪微博
欢迎转载,转载请注明出处
(一)题目
Given a binary tree, flatten it to a linked list in-place.
For example,
Given1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
(二)解题
题目大意:将一个二叉树转换成平树(google来的翻译)
看图就大概知道题目的意思了。
解题思路:flattened tree的顺序就是原树的前序遍历。所以可以定义一颗新树,按照前序遍历来生成右子树。
具体见代码:
/**
* 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:
TreeNode* newroot = NULL;//新树的根节点
TreeNode* p = newroot;//循环中间变量
void flatten(TreeNode* root) {
if(root==NULL) return;
preorder(root);//前序遍历
//这里有个疑惑:为什么roor=newroot不行?
root->right = newroot->right;
root->left = NULL;
}
void preorder(TreeNode* root)//中序遍历
{
TreeNode* temp = new TreeNode(root->val);
if(newroot==NULL) newroot=temp;//保存根节点
else p->right=temp;//构造右子树
p = temp;
if(root->left!=NULL) preorder(root->left);
if(root->right!=NULL) preorder(root->right);
}
};
【一天一道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] 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 将二叉树展平为链表
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 ----- 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将二叉树展成一个链表
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_Medium tag: DFS
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...
- Java for 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 ...
- 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 ...
- Leetcode 114, Flatten Binary Tree to Linked List
根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...
- LeetCode 114. Flatten Binary Tree to Linked List 动态演示
把二叉树先序遍历,变成一个链表,链表的next指针用right代替 用递归的办法先序遍历,递归函数要返回子树变成链表之后的最后一个元素 class Solution { public: void he ...
随机推荐
- python2.7入门---简介&基础语法
Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言,具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有比其他语言更有特色语法结构.基于上述原因, ...
- PAT甲级真题打卡:1001.A+B Format
题目: Calculate a + b and output the sum in standard format -- that is, the digits must be separated i ...
- php+xdebug+dbgp远程调试(多人)
目录 创建 DBGP 服务 配置 调试 创建 DBGP 服务 到 下载页面,下载 python 版本的 dbgp 到服务器上. 解压后执行 pydbgpproxy. 如果提示找不到 dbgp 模块,则 ...
- java Session统计在线用户,并且显示在线用户
关键字: httpsession 1.http://www.jspcn.net/htmlnews/11049329478121583.html 监听器 2.session.invalida ...
- Eclipse创建Maven工程
Eclipse创建Maven工程: Eclipse: New -> Other -> Maven Project -> Next -> webapp -> Finish ...
- Java内存泄漏分析系列之三:jstat命令的使用及VM Thread分析
原文地址:http://www.javatang.com 使用jstat命令 当服务器CPU100%的时候,通过定位占用资源最大的线程定位到 VM Thread: "VM Thread&qu ...
- 2016移动端Android新技术综合预览--好文不多,这一篇就足够
Csdn /Tamic 原文地址: http://blog.csdn.net/sk719887916/article/details/53525067 本文章6月份已完成(http://www.jia ...
- 安卓高级 特效动画ExplosionField和 SmoothTransition
本教程所有图片为github上的所无法正常访问请科学上网 SmoothTransition 展示效果 github:源码地址 使用方法 你能通过一行代码使用上面所有的动画 @Override prot ...
- tomcat内存溢出解决,java.lang.OutOfMemoryError: PermGen space
今天遇到了一个java.lang.OutOfMemoryError: PermGen space异常问题,一直解决不了,根据网上修改了tomcat的配置文件,但是还是解决不了,最后是通过如下方式解决的 ...
- springMVC源码分析--HandlerAdapter(一)
HandlerAdapter的功能实际就是执行我们的具体的Controller.Servlet或者HttpRequestHandler中的方法. 类结构如下: