LeetCode(114) Flatten Binary Tree to Linked List
题目
分析
按要求转换二叉树;
分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树)。
发现规则,便容易处理了。得到先序遍历,构造即可。
AC代码
/**
* 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 || (!root->left && !root->right))
return;
preTraverse(root);
TreeNode *p = root;
//节点个数
int size = preT.size();
for (int i = 1; i < size; ++i)
{
p->left = NULL;
p->right = new TreeNode(preT[i]);
p = p->right;
}
}
public:
//先序遍历
vector<int> preT;
void preTraverse(TreeNode *root)
{
if (!root)
return;
preT.push_back(root->val);
preTraverse(root->left);
preTraverse(root->right);
}
};
LeetCode(114) Flatten Binary Tree to Linked List的更多相关文章
- LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume ...
- LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal
题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume t ...
- LeetCode(24)-Balanced Binary Tree
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- LeetCode(110) Balanced Binary Tree
题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...
- LeetCode(226)Invert Binary Tree
题目 分析 交换二叉树的左右子树. 递归非递归两种方法实现. AC代码 class Solution { public: //递归实现 TreeNode* invertTree(TreeNode* r ...
- leetcode第一刷_ 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题解(二叉树)
Flatten Binary Tree to Linked List: Given a binary tree, flatten it to a linked list in-place. For e ...
- Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)
114. Flatten Binary Tree to Linked List (Medium) 453. Flatten Binary Tree to Linked List (Easy) 解法1: ...
随机推荐
- Metasploits之ms10_018
漏洞详情:https://technet.microsoft.com/library/security/ms10-018 一准备: 1:kali Linux系统 192.168.195.129 2:W ...
- CentOS7下使用Docker容器化.net Core 2.2
一.使用 yum 安装(CentOS 7下) Docker 要求 CentOS 系统的内核版本高于 3.10 ,查看本页面的前提条件来验证你的CentOS 版本是否支持 Docker . 通过 una ...
- 转 如何诊断和解决high version count 10.2.0.4 and 11.2.0.4
转自 http://blog.csdn.net/notbaron/article/details/50927492 在Oracle 10g以上的版本,High version count可谓是一个臭名 ...
- Springboot下事务管理的简单使用
关于事务管理的概念这里就不多介绍了,在我的博客“JDBC事务之理论篇”中也有介绍. 关于Spring的事务管理,主要是通过事务管理器来进行的.这里看个Spring事务管理的接口图:(来自博客https ...
- axios delete 请求
axios delete 请求 在传递一个参数的时候,直接把参数放在请求连接后面,用'/' 连接就可以了 this.axios.post(this.APIURL+'/'+ID) //http://ww ...
- Java编程基础-方法
1.方法(函数)概要 (1).含义:方法(函数)就是定义在类中的具有特定功能的一段独立小程序. (2).方法定义的语法格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参 ...
- sql中的exsits和not exsits
select * from table where exsits(sql语句) : 括号中sql语句有数据则返回这些相关id的数据集 select * from table where not ex ...
- 压力测试工具segie的使用
压力测试工具segie的使用 使用文档参考地址:https://www.joedog.org/siege-manual/ siege4地址:http://download.joedog.org/sie ...
- Android学习总结(十三) ———— ListView 简单用法
一.ListView的基本概念 在Android所有常用的原生控件当中,用法最复杂的应该就是ListView了,它专门用于处理那种内容元素很多,手机屏幕无法展示出所有内容的情况.ListView可以使 ...
- codevs 爱改名的小融
都是三道水题 但我很难理解的是 string 能过 char 就WA 2967 题目描述 Description Wikioi上有个人叫小融,他喜欢改名. 他的名字都是英文,只要按顺序出现R,K,Y三 ...