作者: 负雪明烛
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. 32-3Sum

    相似题目 4sum http://blog.csdn.net/justdoithai/article/details/51195124 http://blog.csdn.net/justdoithai ...

  2. Ansi,UTF8,Unicode,ASCII编码的区别

    Ansi,UTF8,Unicode,ASCII编码的区别 近日需要不同的编码,关于上述编码,一直迷迷糊糊,查了些资料,总算大致了解了, 下面全是从网上搜来的: 1.  ASCII和Ansi编码     ...

  3. Office2020-2021 离线安装教程

    首先:先安装两个 net .再安装 office 如下: DownFile:https://pan.baidu.com/s/19iykxwofXK36wWY5w4GVFg  codenum:6666

  4. jsp的动态包含和静态包含

    jsp的动态包含和静态包含 例如:提取一个公共的页面(top.jsp)到/WEB-INF/jsp/common/目录下 动态包含: 被包含的页面也会独立编译,生成字节码文件,一般包含页面信息频繁变化的 ...

  5. 网易云信 集成UI库登录dologin没有回调

    感谢github上的两位大佬指出问题的解决方法. 解决方法: 在进行ui初始化要在主进程中进行,初始化前进行主进程判断. 若还收不到回调,可尝试将uikit中的base包去掉而在build.gradl ...

  6. 用前端表格技术构建医疗SaaS 解决方案

    电子健康档案(Electronic Health Records, EHR)是将患者在所有医疗机构产生的数据(病历.心电图.医疗影像等)以电子化的方式存储,通过在不同的医疗机构之间共享,让患者面对不同 ...

  7. React 16.13.1触发两次render

    一段很普通的代码,出发了两次render import React, { useState, useEffect } from 'react' const MouseTracker: React.FC ...

  8. 规范——Java后端开发规范

    Java后端开发规范 一.技术栈规约 二.命名规范 三.Java代码规范(注释规范.异常与日志.代码逻辑规范) 四.Mybatis与SQL规范 五.结果检查(单元测试及代码扫描) 六.安全规范 一.技 ...

  9. oracle extract

    select extract(year from systimestamp) year         ,extract(month from systimestamp) month          ...

  10. 【Java 8】 集合间转换工具——Stream.collect

    集合运算 交集 (list1 + list2) List<T> intersect = list1.stream() .filter(list2::contains) .collect(C ...