Given a binary tree, flatten it to a linked list in-place.

For example,
Given 1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6

 思路; 展开后的树其实就是先根遍历的一个结果,不过注意是连接在右子树上。所以先做了一下先根遍历,并保存结果最后按照展开规则处理每个节点。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private :
vector<TreeNode *> temp ;// record the preOrder result
public:
void preOrder(TreeNode *root){
temp.push_back(root);
if(root->left) preOrder(root->left);
if(root->right) preOrder(root->right); }
void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
temp.clear();
if(root == NULL) return ;
preOrder(root);
for(int i = ; i< temp.size()-; i++)
{
temp[i]->right = temp[i+];
temp[i]->left = NULL ;
} }
};

LeetCode_Flatten Binary Tree to Linked List的更多相关文章

  1. [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

    Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...

  2. 【LeetCode OJ】Flatten Binary Tree to Linked List

    Problem Link: http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ The problem is ask ...

  3. 31. 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 ex ...

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

  5. 【LeetCode】Flatten Binary Tree to Linked List

    随笔一记,留做重温! Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-pl ...

  6. 114. Flatten Binary Tree to Linked List(M)

    . Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ...

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

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

随机推荐

  1. CPU卡中T=0通讯协议的分析与实现

    IC卡的应用越来越广泛,从存储卡到逻辑加密卡,目前CPU卡已经逐渐在应用中占据主导地位.CPU卡根据通讯协议可分为两种:接触式和非接触式.接触式CPU卡主要采用两种通讯协议:T=0和T=1通讯协议.T ...

  2. Qt编程之在QGraphics scene中使用图片

    http://stackoverflow.com/questions/5960074/qimage-in-a-qgraphics-scene http://stackoverflow.com/ques ...

  3. 必须用C模拟OS?

    ASM基本必要,至于高级语言就很难说了.去osdev wiki上一翻一堆各种语言实现的玩意. 一个模拟OS其实不太容易完整搭出来,反倒是直接构造内核的后顾之忧少(如果还有真的想在SIGALRM里耍什么 ...

  4. 学习javascript基础知识系列第二节 - this用法

    通过一段代码学习javascript基础知识系列 第二节 - this用法 this是面向对象语言中的一个重要概念,在JAVA,C#等大型语言中,this固定指向运行时的当前对象.但是在javascr ...

  5. 关于target is null for setProperty的问题总结

    出现了这个问题,报错是 com.opensymphony.xwork2.ognl.OgnlValueStack WARN  - Error setting expression 'costRecord ...

  6. 《javascript设计模式》读书笔记四(单例模式)

    1.单利模式简单介绍 在<设计模式>中单利模式是一种比較简单的模式,定义例如以下: 确保某一个类仅仅有一个实例,并且自行实例化并向整个系统提供这个实例. 在javascript中则将代码组 ...

  7. C# winform带进度条的图片下载

    代码如下: public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } private void ...

  8. Linux的目录结构及其作用

    /bin bin是Binary的缩写.这个目录存放着最经常使用的命令. /boot这里存放的是启动Linux时使用的一些核心文件,包括一些连接文件以及镜像文件. /dev dev是Device(设备) ...

  9. 无法从带有索引像素格式的图像创建graphics对象(转)

    大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be ...

  10. ajax例子

    <!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...