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: ...
随机推荐
- 2017浙江工业大学-校赛决赛 小M和天平
Description 小M想知道某件物品的重量,但是摆在他面前的只有一个天平(没有游标)和一堆石子,石子可以放左边也可以放右边.他现在知道每个石子的重量.问能不能根据上述条件,能不能测出所问的重量. ...
- php 分析2
a:link,a:visited,a:hover,a:active 1:解释 link:连接平常的状态 visited:连接被访问过之后 hover:鼠标放到连接上的时候 active:连接被按下 ...
- Exception in thread "main" java.lang.SecurityException: class "javax.servlet.FilterRegistration"'s signer information does not match signer information of other classes in the same package解决办法(图文详解)
不多说,直接上干货! 问题详情 SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF ...
- 正则表达式匹配URL——给URL地址加上<a> 链接
<?php function replace_url ($content) { if (empty($content)) return; //给URL地址加上 <a> 链接 $pre ...
- C++使用ADO连接数据库及其实例
读写数据库的技术很多,现在多用ADO.ADO以COM方式提供,所以它的很多行为遵循COM规范.首先,要引入ADO的COM文件,它的位置一般在"C:/Program Files/Common ...
- GreenDao3.2的使用
原文:http://blog.csdn.net/qq_30379689/article/details/54410838 GreenDao3.2的使用,爱不释手 本篇文章包括以下内容: 前言 Gree ...
- 【MATLAB 从零到进阶】day2 矩阵 数组
访问矩阵元素 >> A=[1,2,3;4,5,6;7,8,9]; >> x=A(2,3)% 双下标访问 x = 6 >> x=A(2)% 单下标访问 x = 4 单 ...
- C# 一维数组 冒泡排序
假设有个三个杯子 一个杯子中有一个紫色的乒乓球 一个没有 一个有红色乒乓球 杯子不能动 怎么把紫色和红色的调换呢 主要是先把紫色的放到空的杯子 在把红的放到紫色原来的杯子 再把 ...
- codevs 3278 最小m 段和问题
时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定 n 个整数(不一定是正整数)组成的序列,现在要求将序列分割为 m 段,每段 ...
- UESTC cdoj 619 吴神,人类的希望 (组合数学)
枚举盒子的个数,先把总数n减去掉box*k保证每个盒子至少有k个小球,剩下的小球放入盒子中可以为空, 加入box个小球保证每个盒子至少有一个小球,问题转化成不可区分小球放入不可区分盒子非空的方案数. ...