【一天一道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 6The 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 ...
随机推荐
- Lua和C#调用探秘
转载请标明出处:http://www.cnblogs.com/zblade/ 在实际的项目中,大部分业务逻辑 程序员只需要负责lua层编写逻辑即可,或者在c#层添加一些静态函数,供lua层调用.那么对 ...
- Mysql优化--Show Profile
Mysql 系列文章主页 =============== 是Mysql提供可以用来分析当前会话中语句执行的资源消耗情况.可以用于Sql的调优的测量.默认情况下处于关闭状态,并保存最近 15 次的运行结 ...
- 数据结构之堆Heap
1. 概述 堆(也叫优先队列),是一棵完全二叉树,它的特点是父节点的值大于(小于)两个子节点的值(分别称为大顶堆和小顶堆).它常用于管理算法执行过程中的信息,应用场景包括堆排序,优先队列等. 2. 堆 ...
- 关于Node.js中HTTP请求返回数据需要JSON解析的问题
在编写项目过程中,需要用到实时数据的推送需求, 所以首先想到了NodeJS的websocket模块 在网上找了一个聊天室的例子 然后将其改为自己需求的推送 其中遇到的问题 返回数据问题 : 由 ...
- Kinect2.0 MultiSourceFrameReader 的 AcquireLatestFrame 方法获取不到帧的解决方案
先把大致要写的东西写一下,手里的活忙完了再完善. 在代码中使用下边的语句,获取Kinect中,colorFrame, depthFrame, bodyIndex三种帧,但是经常会遇到在后边的程序中处理 ...
- python中的printf:%号拼接字符串和format函数
在C语言中,我们使用printf("%s","hello")这种形式进行字符串的拼接 在python中,进行这样的拼接有两种实现方式,分别是%号拼接以及使用fo ...
- 含有分类变量(categorical variable)的逻辑回归(logistic regression)中虚拟变量(哑变量,dummy variable)的理解
版权声明:本文为博主原创文章,博客地址:,欢迎大家相互转载交流. 使用R语言做逻辑回归的时候,当自变量中有分类变量(大于两个)的时候,对于回归模型的结果有一点困惑,搜索相关知识发现不少人也有相同的疑问 ...
- C++后台实践:古老的CGI与Web开发
本文写给C/C++程序猿,也适合其他对历史感兴趣的程序猿 ============================================= 谈到web开发,大家首先想到的PHP.JavaEE ...
- A discussion of Dead Connection Detection, Resource Limits, V$SESSION, V$PROCESS and OS processes
A discussion of Dead Connection Detection, Resource Limits, V$SESSION, V$PROCESS and OS processes (文 ...
- django+uwsgi+nginx+postgresql备忘
安装pg创建数据库xxx设置用户密码111111 apt-get install postgresql su - postgres psql create database xxx; alter us ...