题目

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
解题思路:利用递归找到倒数第一个父节点。记录下它的右节点,将左边的移到右边。然后再把之前标记的右节点连接上。

代码

public class Solution {
public void flatten(TreeNode root) {
if(root==null) return;
flatten(root.left);
flatten(root.right);
TreeNode temp=root.right;
if(root.left!=null){
root.right=root.left;
root.left=null; while(root.right != null){
root=root.right;
}
root.right=temp; } }
}

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List的更多相关文章

  1. 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. 将二 ...

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

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

  3. 【LeetCode】114. 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. 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 ...

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

  6. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

  7. 【一天一道LeetCode】#114. Flatten Binary Tree to Linked List

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

随机推荐

  1. Python编程快速上手--实践项目11.11.1

    from selenium import webdriver from selenium.webdriver.common.keys import Keys import time def messa ...

  2. Solr5.0.0 DIH之增量索引

    定时索引相关知识 增量更新需要配置个sql(deltaImportQuery.deltaQuery) deltaImportQuery="select * where id='${dih.d ...

  3. C语言文件操作 FILE结构体

    内存中的数据都是暂时的,当程序结束时,它们都将丢失.为了永久性的保存大量的数据,C语言提供了对文件的操作. 1.文件和流 C将每个文件简单地作为顺序字节流(如下图).每个文件用文件结束符结束,或者在特 ...

  4. C++ 实验六

    Part.2 // 合并两个文件内容到一个新文件中. // 文件名均从键盘输入 #include <iostream> #include <fstream> #include ...

  5. leetcode刷题——动态规划

    知识点 专题-B-动态规划 题目 斐波那契数列 矩阵路径 数组区间 分割整数 最长递增子序列 最大连续子序列和 最长公共子序列 最长回文子序列 最长公共子串 最长回文子串 背包 题解 CS-Notes ...

  6. TCP三次握手及四次挥手详解及常见面试题

    https://blog.csdn.net/ZWE7616175/article/details/80432486

  7. Excel读取导入数据库碰到的问题

    1.未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序. 下载并安装驱动:http://download.microsoft.com/download/7/0/3/703 ...

  8. Python --链接Mongodb

    # -*- coding: UTF-8 -*- from pymongo import MongoClient # 数据库连接 class MongoDB(object): def __init__( ...

  9. [uiautomator篇] python uiautomatorDemo

    #coding=utf-8 """ @version: 1.0 @author: @prerequisite: based on Python 2.7 @usage: 1 ...

  10. Masonry练习

    tableView的cell自动适应,scrollview自动适应,自定义自动布局控件 demo链接:http://pan.baidu.com/s/1jHsrGwQ