【一天一道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 ...
随机推荐
- 用 ConfigMap 管理配置 - 每天5分钟玩转 Docker 容器技术(159)
Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap. ConfigMap 的创建和使用方式与 Secret 非常类 ...
- 使用RestTemplate访问restful服务时遇到的问题
可以通过通过wireshark抓包,使用Postman发送请求 wireshark是非常流行的网络封包分析软件,功能十分强大.可以截取各种网络封包,显示网络封包的详细信息.使用wireshark的人必 ...
- RedisAsyncClientAdapter-------------接口继承
public abstract class RedisAsyncClientAdapter<K, V, T extends RedisKeyAsyncCommands<K, V> & ...
- Ubuntu 14.04 16.04 17.10 + Win10 双系统安装记录 + 分区大小选择办法
安装了N遍,重要的东西在此记录. 参考了 http://www.libinx.com/2017/five-steps-win10-ubuntu-dual-boot/ 忠告:为了让日后喘气能匀呼些,要选 ...
- 63. Unique Paths II(中等, 能独立做出来的DP类第二个题^^)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- [HCNA]VLAN配置Hybrid接口
实验名称 VLAN配置Hybrid接口 日期 2018年4月13日 实验目的 1.掌握配置Hybrid接口的方法. 2.理解Hybrid接口处理Untagged数据帧过程 3.理解Hybrid接口处理 ...
- C# get 、set、索引器
get 与 set C#类的属性有公有属性(public)和私有属性(private).如果直接将一个属性声明为public,则该类的任意实例可以随意获取或修改该属性的值,很不安全..NET Fram ...
- 使用gogs搭建git私有仓库
搭建gogs 我的机器环境:centos 7 1.安装git yum install git 2.安装mysql gogs的数据存在mysql中,需要安装一个mysql来存数据,当然也有其他的选择 ...
- Answers to "Why are my jobs not running?"
from :https://community.oracle.com/thread/648581 This is one of the most common Scheduler questions ...
- 防止SpringMVC拦截器拦截js等静态资源文件
SpringMVC提供<mvc:resources>来设置静态资源,但是增加该设置如果采用通配符的方式增加拦截器的话仍然会被拦截器拦截,可采用如下方案进行解决: 方案一.拦截器中增加针对静 ...