题目

分析

按要求转换二叉树;

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

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

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. python学习之邮件

    分类smtp邮件 html邮件,带附件的邮件. 一 STTP邮件:设置SMTP代理机构,发送人的邮箱和密码,收件人的邮箱地址(email模块):接收端,发送(smtplib模块).SMTP(smtp_ ...

  2. java中的线程安全是什么?什么叫线程安全?什么叫不安全?

    java中的线程安全是什么: 就是线程同步的意思,就是当一个程序对一个线程安全的方法或者语句进行访问的时候,其他的不能再对他进行操作了,必须等到这次访问结束以后才能对这个线程安全的方法进行访问 什么叫 ...

  3. Fedora如何添加第三方软件源?

    安装RPM Fusion源 和 安装FZUG源 http://jingyan.baidu.com/article/656db918f9300ae380249c56.html

  4. CF1072B Curiosity Has No Limits

    思路: 对于序列t,只要第一个数确定了,后续的数也随之确定了.枚举四种情况即可.实现: #include <iostream> #include <vector> using ...

  5. webpack入门之最简单的例子 webpack4

    webpack在目前来说应该是前端用的比较多的打包工具了,那么对于之前没有接触过这块的该怎么办呢?答案很明显嘛,看资料,查文档,自己去琢磨,自己去敲一敲,跑一跑: 那么,这边我将以一个最基础的例子来将 ...

  6. Handler消息机制的一些原理(直接用code讲解)——Android开发

    package com.example.handlertest; import android.os.Bundle; import android.os.Handler; import android ...

  7. uvm.sv——UVM之道

    文件: $UVM_HOME/src/uvm.sv 类: 无   `include "uvm_pkg.sv"   Thus spake the UVM master programm ...

  8. SQL根据出生日期精确计算年龄、获取日期中的年份、月份

    第一种: 一张人员信息表里有一人生日(Birthday)列,跟据这个列,算出该人员的年龄 datediff(year,birthday,getdate()) 例:birthday = '2003-3- ...

  9. Java中枚举类型Enum的一种使用方式

    枚举类定义如下: public enum Status { SCUUESS("1", "成功"), FAILED("2", "失败 ...

  10. SQL 数学串函数

    数学函数 ceiling  取上限 floor  取下限 round 四舍五入 len   长度 abs  绝对值 PI()圆周率 sqrt 开根号 qwuare 平方根 select  10     ...