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
Hints:

If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

原题链接:https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/

题目:给定二叉树,按前序位置展平成一个链表。

思路:递归处理。把右子树放到左子树之后,并清空左子树。

	public void flatten(TreeNode root) {
if(root == null)
return;
flatten(root.left);
flatten(root.right);
TreeNode tmp = root;
if(tmp.left == null)
return;
else
tmp = tmp.left;
while(tmp.right != null)
tmp = tmp.right;
tmp.right = root.right;
root.right = root.left;
root.left = null;
}
// Definition for binary tree
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}

相同的做法。可是非递归。

	public void flatten(TreeNode root){
while(root != null){
if(root.left != null){
TreeNode tmp = root.left;
while(tmp.right != null)
tmp = tmp.right;
tmp.right = root.right;
root.right = root.left;
root.left = null;
}
root = root.right;
}
}

reference : http://blog.csdn.net/perfect8886/article/details/20000083

版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode——Flatten Binary Tree to Linked List的更多相关文章

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

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

  3. [LeetCode] 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 ...

  4. [leetcode]Flatten Binary Tree to Linked List @ Python

    原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...

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

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

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

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

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

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

随机推荐

  1. Highcharts中文教程

    http://www.hcharts.cn/docs/index.php?doc=start

  2. hdu3572 任务分配/最大流量推论全流

    意甲冠军:将n分配的任务m机.到的每个任务需要的天数(如果没有持续的日常),并能做到在哪些天任务.询问是否有计划. 典型的任务(X)----日(Y)一半的最大流量,(因为这个任务是天之间的关系)处理器 ...

  3. ios 多线程开发(一)简介

    简介 线程是在一个程序中并发的执行代码的方法之一.虽然有一些新的技术(operations, GCD)提供了更先进高效的并发实现,OS X和iOS同时也提供了创建和维护线程的接口. 这里将要介绍线程相 ...

  4. Linux lspci查看硬件设备

    Linux 主机的硬件配备 lspci 找到的是眼下主机上面的硬件配备 [root@www ~]# lspci [-vvn] 选项与參数: -v     :显示很多其它的 PCI 接口装置的具体信息 ...

  5. 顺序表----java实现

    最简单的数据结构--顺序表,此处以数组为例. 顺序表的优点:支持随机读取,内存空间利用率高. 顺序表的缺点:1.需要预先给出最大数据元素个数,这往往很难实现. 2.插入和删除时需要移动大量数据. Se ...

  6. rhel6使用的版本数部分intel xeon处理器时间bug

    可惜在总前几天"oracle操作和维护的高级别小组"于.BBQ 上帝说,大量RHEL的bug.这bug在这个例子中,下面的URL: https://access.redhat.co ...

  7. 编程算法 - 数字数组中只出现一次 代码(C)

    数字数组中只出现一次 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 一个整型数组里除了两个数字以外, 其它的数字都出现了两次. 请敲代码找出这 ...

  8. hdu4362 dp + 单调队列优化

    dp传输方程很easy需要 dp[i][j] = min{dp[i - 1][k] + abs(pos[i][j] -pos[i - 1][j]) + cost[i][j]} n行m一排 每个传输扫描 ...

  9. Android 应用程序启动过程源代码分析

    本文转自:http://blog.csdn.net/luoshengyang/article/details/6689748 前文简要介绍了Android应用程序的Activity的启动过程.在And ...

  10. 深入理解Android中ViewGroup

    文章目录   [隐藏] 一.ViewGroup是什么? 二.ViewGroup这个容器 2.1 添加View的算法 2.1.1 我们先来分析addViewInner方法: 2.1.2 addInArr ...