题目

分析

按要求转换二叉树;

分析转换要求,发现,新的二叉树是按照原二叉树的先序遍历结果构造的单支二叉树(只有右子树)。

发现规则,便容易处理了。得到先序遍历,构造即可。

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);
} };

GitHub测试程序源码

LeetCode(114) Flatten Binary Tree to Linked List的更多相关文章

  1. 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 ...

  2. 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 ...

  3. LeetCode(24)-Balanced Binary Tree

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  4. LeetCode(110) Balanced Binary Tree

    题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bina ...

  5. LeetCode(226)Invert Binary Tree

    题目 分析 交换二叉树的左右子树. 递归非递归两种方法实现. AC代码 class Solution { public: //递归实现 TreeNode* invertTree(TreeNode* r ...

  6. leetcode第一刷_ Flatten Binary Tree to Linked List

    提示中说明了,改动后的链表相当于原树的前序遍历结果.前序遍历是根左右,因为要把转换后的左子树链接到根节点的右子树上,因此进入递归之后要先把节点的右子树保存下来,然后进入左子树,左子树转换后应该返回最后 ...

  7. LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)

    题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...

  8. [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 ...

  9. 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: ...

随机推荐

  1. Codeforces Round #541 (Div. 2) C.Birthday

    链接:https://codeforces.com/contest/1131/problem/C 题意: 求给的n个数,相邻差值最小的排列方式.1-n相邻. 思路: sort后隔一个取一个,取到底后再 ...

  2. 牛客寒假5-A.炫酷双截棍

    链接:https://ac.nowcoder.com/acm/contest/331/A 题意: 小希现在手里有一个连着的两块木条,长度分别为l1l1,l2l2,木条之间有一个无摩擦的连接点,木条之间 ...

  3. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C

    Misha and Vanya have played several table tennis sets. Each set consists of several serves, each ser ...

  4. bzoj4059 [Cerc2012]Non-boring sequences && bzoj5200 [NWERC2017]Factor-Free Tree

    https://konnyakuxzy.github.io/BZPRO/JudgeOnline/4059.html https://cn.vjudge.net/problem/Gym-100624D ...

  5. 15 使用lambdas和闭包

    1       使用lambdas和闭包 1.1  定义闭包 闭包是一个代码块,代替了方法或类. groovy中的闭包基于后边的构造方式:{list of parameters-> closur ...

  6. 简单了解junit的使用

    普通使用: 在没有用测试框架之前,我们要用一个main方法来跑代码,而有了像junit这样的测试框架后,就可以不用次次写个main方法了. 直接上代码,注释里有说明: package com.stuP ...

  7. python学习之结构语句

    一 循环语句: 1.1 for x in rang(n) :#rang(n)生成左闭右开区间的序列 1.2 while x 条件n: 二条件语句: if 条件表达式: elif 表达式: elif 表 ...

  8. Python基础之collection

    collection-系列 cellection是作为字典.元组(列表与元组可互相转换)的扩充,在此需要导入cellection 一.计数器(counter) counter是对字典类型的补充,用户获 ...

  9. phpdesigner 配置SVN

  10. 分布式系统ID生成办法

    前言 一般单机或者单数据库的项目可能规模比较小,适应的场景也比较有限,平台的访问量和业务量都较小,业务ID的生成方式比较原始但是够用,它并没有给这样的系统带来问题和瓶颈,所以这种情况下我们并没有对此给 ...