作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/flatten-binary-tree-to-linked-list/#/description

题目描述

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

题目大意

把一个二叉树拉直,即按照先序遍历的顺序形成一个只有右孩子的二叉树。

解题方法

先序遍历

最简单的方法就是使用列表保存先序遍历的每个节点,然后在列表中完成操作。即,使得列表中每个元素的左孩子为空,右孩子都是下一个节点。

这个方法很简单,不过需要O(N)的空间复杂度。

python代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
res = []
self.preOrder(root, res)
for i in range(len(res) - 1):
res[i].left = None
res[i].right = res[i + 1] def preOrder(self, root, res):
if not root: return
res.append(root)
self.preOrder(root.left, res)
self.preOrder(root.right, res)

递归

递归一定要明白递归函数的意义,我觉得如果不清楚的弄明白递归函数的输入和输出是什么,那么不可能写出正确的代码。

这里的flatten(root)的输入是一个树的根节点,这个函数将使得该根节点下的所有孩子按照先序遍历的顺序放到其右侧。返回是空,但是这个函数运行结束后,root的指向仍然是原来的位置,即树的根节点。

所以,递归的思路就有了:把左右子树分别flatten形成两个链表,然后把根节点的左孩子放到根节点的右孩子上。把原先的根节点的右孩子拼到当前根节点链表的结尾。

图形化说明:

     1
/ \
2 5
/ \ \
3 4 6

变成:

     1
\
2 5
\ \
3 4 6

2是root,3是left,4是right。修改成下面这样:

再变成:

     1
\
2 5
\ \
3 6
\
4

再变成:

     1
\
2
\
3
\
4
\
5
\
6

python代码:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def flatten(self, root):
"""
:type root: TreeNode
:rtype: void Do not return anything, modify root in-place instead.
"""
if not root: return
left = root.left
right = root.right
root.left = None
self.flatten(left)
self.flatten(right)
root.right = left
cur = root
while root.right:
root = root.right
root.right = right

C++代码:

/**
* 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:
void flatten(TreeNode* root) {
if (!root) return;
TreeNode* left = root->left;
TreeNode* right = root->right;
root->left = nullptr;
flatten(left);
flatten(right);
root->right = left;
TreeNode* cur = root;
while (cur->right)
cur = cur->right;
cur->right = right;
}
};

Java代码:

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

日期

2017 年 4 月 19 日
2019 年 1 月 7 日 —— 新的一周开始啦啦啊

【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)的更多相关文章

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

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

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

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

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

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

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

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

随机推荐

  1. Yii自定义全局异常,接管系统异常

    Yii自定义全局异常,接管系统异常 一般自己的框架都会使用一些自己封装的全局异常,那么在系统发生异常突发情况时候,即可自主的做一些异常机制处理,例如发送短信.发送邮件通知系统维护人员或者以更加友好的方 ...

  2. EXCEL-名称管理器

    1.怎么用? 两种方法 参考:https://jingyan.baidu.com/article/a378c960a26f26b3282830a6.html 2.有什么功能? (1)直接引用或者函数直 ...

  3. 8.Maximum Depth of Binary Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  4. 听老外吐槽框架设计,Why I Hate Frameworks?

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. Hello,小伙伴们,今天不聊技术,分享点有意思的东西.前段时间,表弟给我发过来一篇老外写的文章,以略带讽刺的对话方式调侃了自己对框架的看法,我 ...

  5. Elaticsearch(一)--基础原理及用法

    一.基础概念 1.Elasticsearch简介 Lucene是Java语言编写的全文(全部的文本内容进行分析,建立索引,使之可以被搜索)检索引擎工具包(全文检索引擎的架构),用于处理纯文本的数据,提 ...

  6. day11 序列化组件、批量出入、自定义分页器

    day11 序列化组件.批量出入.自定义分页器 今日内容详细 ajax实现删除二次提醒(普通版本) ajax结合第三方插件sweetalert实现二次提醒(样式好看些) ajax如何发送文件数据 aj ...

  7. Spark Stage 的划分

    Spark作业调度 对RDD的操作分为transformation和action两类,真正的作业提交运行发生在action之后,调用action之后会将对原始输入数据的所有transformation ...

  8. 零基础学习java------day18------properties集合,多线程(线程和进程,多线程的实现,线程中的方法,线程的声明周期,线程安全问题,wait/notify.notifyAll,死锁,线程池),

    1.Properties集合 1.1 概述: Properties类表示了一个持久的属性集.Properties可保存在流中或从流中加载.属性列表中每个键及其对应值都是一个字符串 一个属性列表可包含另 ...

  9. Sharding-JDBC 实现水平分表

    1.搭建环 (1) 技术: SpringBoot2.2.1+ MyBatisPlus + Sharding-JDBC + Druid 连接池(2)创建 SpringBoot 工程

  10. 在服务端应用中如何获得客户端 IP

    如果有 x-forwarded-for 的请求头,则取其中的第一个 IP,否则取建立连接 socket 的 remoteAddr. 而 x-forwarded-for 基本已成为了基于 proxy 的 ...