将左子树接到右子树之前,递归解决

void flatten(TreeNode *root)
{
if (root == nullptr)return; flatten(root->left);
flatten(root->right);
//如果没有左子树,直接返回即可
if (root->left == nullptr)return;
p = root->left;
//寻找左子树的最后一个结点
while (p->right)p = p->right;
//将右结点接在左子树的最后一个结点上
p->right = root->right;
//将左子树移到右子树上
root->right = root->left;
root->left = nullptr;
}

Leetcode 之Flatten Binary Tree to Linked List(50)的更多相关文章

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

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

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

  3. leetcode dfs Flatten Binary Tree to Linked List

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

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

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

  6. [Leetcode][JAVA] 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】Flatten Binary Tree to Linked List (middle)

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

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

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

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

随机推荐

  1. [USACO06NOV]玉米田Corn Fields 状压DP

    题面: 农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的 ...

  2. BZOJ1588:[HNOI2002]营业额统计——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1588 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务 ...

  3. BZOJ2823 [AHOI2012]信号塔 【最小圆覆盖】

    题目链接 BZOJ2823 题解 最小圆覆盖模板 都懒得再写一次 #include<iostream> #include<cstdio> #include<cmath&g ...

  4. 专题训练之2-sat

    推荐几篇博客:https://blog.csdn.net/JarjingX/article/details/8521690 研究总结2-sat问题 https://blog.csdn.net/wher ...

  5. JavaScript转换与解析JSON的方法

    在JavaScript中将JSON的字符串解析成JSON数据格式,一般有两种方式: 一种为使用eval()函数. 使用Function对象来进行返回解析. 使用eval函数来解析,jquery的eac ...

  6. hdoj 1299 Diophantus of Alexandria

    hdoj 1299 Diophantus of Alexandria 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1299 题意:求 1/x + 1/y ...

  7. mybatis中association和collection的column传入多个参数值

    在使用 association和collection 进行关联查询的时候 column 参数可能会有多个,如下: 注意: parameterType 一定要是 java.util.Map

  8. 9.python爬虫--pyspider

    pyspider简介 PySpider:一个国人编写的强大的网络爬虫系统并带有强大的WebUI.采用Python语言编写,分布式架构,支持多种数据库后端,强大的WebUI支持脚本编辑器,任务监视器,项 ...

  9. Eclipse中安装Tomcat

    1. 下载Tomcat并安装: http://tomcat.apache.org/download-60.cgi 2. 下载最新Eclipse的Tomacat插件: http://www.eclips ...

  10. asyncio 实现 aiohttp

    #asyncio 没有提供http协议的接口 aiohttp import asyncio import socket from urllib.parse import urlparse async ...