给定一个二叉树,原地将它展开为链表。

例如,给定二叉树

1 / \ 2 5 / \ \ 3 4 6

将其展开为:

1 \ 2 \ 3 \ 4 \ 5 \ 6

class Solution {
public:
TreeNode *last = NULL;
void flatten(TreeNode* root)
{
if(root == NULL)
return;
TreeNode *r = root ->right;
if(last != NULL)
{
last ->right = root;
}
last = root;
flatten(root ->left);
flatten(r);
root ->left = NULL;
}
};

Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表的更多相关文章

  1. 114 Flatten Binary Tree to Linked List 二叉树转换链表

    给定一个二叉树,使用原地算法将它 “压扁” 成链表.示例:给出:         1        / \       2   5      / \   \     3   4   6压扁后变成如下: ...

  2. LeetCode114 Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. (Medium) For example,Given 1 / \ 2 5 / \ ...

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

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

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

  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-place. For e ...

  6. leetcode dfs Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...

  7. leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree

    1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...

  8. 114 Flatten Binary Tree to Linked List [Python]

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

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

随机推荐

  1. Mid-Atlantic 2008 Lawrence of Arabia /// 区间DP oj21080

    题目大意: 输入n,m 输入n个数 将n个数切割m次分为m+1段,使得各段的Strategic Value总和最小 一组数a b c d的SV值为 a*b + a*c + a*d + b*c + b* ...

  2. [WPF自定义控件库] 让Form在加载后自动获得焦点

    原文:[WPF自定义控件库] 让Form在加载后自动获得焦点 1. 需求 加载后让第一个输入框或者焦点是个很基本的功能,典型的如"登录"对话框.一般来说"登录" ...

  3. 编写Reduce处理逻辑

  4. spring:ApplicationContext的三个实现类

    * ApplicationContest的三个常用实现类* ClassPathXmlApplicationContext:它可以加载类路径的配置文件,要求配置文件必须在类路径下,如果不在则加载不了* ...

  5. 杜教筛&套路总结

    杜教筛 \[ \begin{split} (g*f)(i)&=\sum_{d|i}g(d)f(\frac id)\\ \Rightarrow g(1)S(n)&=\sum_{i=1}^ ...

  6. php结合phpStudy实例来熟悉CI框架,用的软件是phpStorm+phpStudy

    1.新建项目名字,我的是放在E盘,叫test,主要是包括application,system,index.php.我的控制器和视图不想放在application中,所以我新建了一个文件夹叫phpTes ...

  7. java基础温习 -- Thread

    启动线程两种方式: 1. 实现Runnable接口: 2. 继承Thread类. 选用:能使用接口,就不用从Thread类继承.    使用继承的方法不够灵活,从这个类继承了就不能从其他类继承: 实现 ...

  8. 11-3-while

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Compile_Netgen_WITH_OpenCascade

    title: Compile_Netgen_WITH_OpenCascade date: 2016-04-23 21:14:42 tags: 结合OCCT编译Netgen date: 2016-04- ...

  10. Python学习之--python概要

    1 Python的优点 Python语言类库齐全,语法简洁,而且在linux上自带安装,在处理大数据以及自动化方面有其独有的特点.2 Python的解释器 Python解释器用来解释python代码, ...