题目

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

例如,给定二叉树

    1
/ \
2 5
/ \ \
3 4 6 将其展开为: 1
\
2
\
3
\
4
\
5
\
6

解析

  • 通过递归实现;可以用先序遍历,然后串成链表
  • 主要思想就是:先递归对右子树进行链表化并记录,然后将root->right指向 左子树进行链表化后的头结点,然后一直向右遍历子树,连接上之前的右子树
/**
* 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:
TreeNode* treetolist(TreeNode* root)
{
if(!root)
return NULL;
TreeNode* right=treetolist(root->right); //记录右子树
root->right=treetolist(root->left);
root->left=NULL; TreeNode* cur=root;
while(cur->right)
{
cur=cur->right;
}
cur->right=right;
return root;
}
void flatten(TreeNode* root) { treetolist(root);
}
};

LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)的更多相关文章

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

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  2. [LeetCode] 114. Flatten Binary Tree to Linked List 将二叉树展平为链表

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  3. leetcode 114 Flatten Binary Tree to Linked List ----- java

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  4. [leetcode]114. Flatten Binary Tree to Linked List将二叉树展成一个链表

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  5. [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  6. Java for LeetCode 114 Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...

  7. leetcode 114.Flatten Binary Tree to Linked List (将二叉树转换链表) 解题思路和方法

    Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...

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

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

  9. Leetcode 114, Flatten Binary Tree to Linked List

    根据提示,本题等价于pre order traverse遍历,并且依次把所有的节点都存成right child,并把left child定义成空集.用递归的思想,那么如果分别把左右子树flatten成 ...

随机推荐

  1. 正睿OI 提高 Day1T3 ZYB玩字符串(DP)

    题目链接 设可能的答案串为p,长为len.p一定是s的一个子串且len|n. 虽然一些p在s中可能被断成若干段,但删掉其中的若干段后,这段区间一定会被全部消掉. 于是枚举p后,可以用f[i][j]表示 ...

  2. JAVA线程和进程区别

    1,JAVA线程和进程区别? (1)简单来讲一个运行的程序就是一个进程,一个进程中可以有多个线程(线程是程序执行的最小单元). (2)线程有四种状态:运行,就绪,挂起,结束 (3)使用多线程的好处 使 ...

  3. JVM7、8参数详解及优化

    1. JVM堆内存划分 这两天看到下面这篇文章的图不错. 一图读懂JVM架构解析 1.1 JDK7及以前的版本 其中最上一层是Nursery内存,一个对象被创建以后首先被放到Nursery中的Eden ...

  4. phpexcel错误 You tried to set a sheet active by the out of bounds index: 1解决办法

    $objPHPExcel->createSheet($k);

  5. 关于tensorflow中tensorborad No dashboards are active for the current data set.的解决办法

    说明:这个问题,困惑了好久,在网上查了很久,一直没能解决,直到我在stackoverflow上看到有一位博主的回答 链接在这里:(https://stackoverflow.com/questions ...

  6. RC4加密算法的原理及实现

    RC4于1987年提出,和DES算法一样.是一种对称加密算法,也就是说使用的密钥为单钥(或称为私钥). 但不同于DES的是.RC4不是对明文进行分组处理,而是字节流的方式依次加密明文中的每个字节.解密 ...

  7. 【Go命令教程】13. go tool cgo

    cgo 也是一个 Go 语言自带的特殊工具.一般情况下,我们使用命令 go tool cgo 来运行它.这个工具可以使我们创建能够调用 C 语言代码的 Go 语言源码文件.这使得我们可以使用 Go 语 ...

  8. man命令使用

    如:man 2 read, 就可以查看read函数的文档

  9. 安装oracle10g“程序异常终止。发生内部错误。请将以下文件提供给oracle技术支持部门

    发生情景:测试环境搭建的是windows 2008 r2 sp1系统 在安装Oracle 10g数据库时发生了错误,现在把解决问题的方法和原因分享给大家. *  安装出现的现象: 输入完密码后下一步时 ...

  10. Win10正式版开机慢怎么办 开机黑屏时间长怎么办

    升级Win10正式版后开机速度慢.黑屏时间长怎么解决呢?其实我重要是由Win10正式版所提供的“快速启动”功能与电脑显卡驱动.电源管理驱动不兼容所造成的.下面就与大家分享一下针对Win10正式版开机速 ...