114 Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.

将二叉树展开成链表

[]

(D:\dataStructure\Leetcode\114.png)

思路:将根节点与左子树相连,再与右子树相连。递归地在每个节点的左右孩子节点上,分别进行这样的操作。

代码

class Solution(object):
def flatten(self, root):
if root == None:
return
if root.left == None and root.right == None:
return
self.flatten(root.left)
self.flatten(root.right)
tmp = root.left
root.right = root.left
root.left = None
while root.right:
root = root.right
root.right = tmp

114 Flatten Binary Tree to Linked List [Python]的更多相关文章

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

  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. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

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

  5. 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先序遍历 递归 日期 题目地址:https://le ...

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

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

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

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

随机推荐

  1. sql性能优化浅谈

    sql性能优化总结: 最近随着数据越来越多,数据库性能问题暴露的越来越严重.几百万,上千万,甚至过亿的数据处理速度会非常的慢. 下面对工作中遇到的问题做下总结,希望以后能对日后的工作有所帮助. 不同的 ...

  2. Elegy written in a country church-yard

    分享一首好诗:托马斯·格雷的<墓地哀歌>. "ELEGY WRITTEN IN A COUNTRY CHURCH-YARD" The curfew tolls the ...

  3. 使用Navicat for Oracle工具连接oracle出错:ORA-12737

    今天上网的时候偶然发现了一款oracle的客户端的图形化管理和开发工具,当看到这个界面的时候,感觉很舒服,便上网搜了一下这个工具,看百度百科之后感觉很出乎我的意料,这个产品对于许多的数据库竟都有支持, ...

  4. 将项目导入myeclipse后 tortoise svn 右键项目不能更新和提交

    使用 tortoise svn客户端将svn服务器上的项目checkout之后正常,可以更新也可以提交:当将这个项目导入MyEclipse之后,不能更新和提交了只出现svn升级工作副本这一字样:网上搜 ...

  5. scatter散点图

    import matplotlib.pyplot as plt import numpy as np n = 1024 X = np.random.normal(0,1,n) Y = np.rando ...

  6. Flutter Widgets 之 SnackBar

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 基础用法 应用程序有时候需要弹出消息提示用户,比如'网络连 ...

  7. vim 编辑器技巧 打开多窗口编辑 vsp

    我有两个配置文件 [root@gameserver1 conf]# ls auth_1.json auth_2.json 先打开auth_2.json 在vim编辑器中打开auth_1.json,在打 ...

  8. git指令-工作区和暂存区

    #git指令-工作区和暂存区 工作区(Working Directory):就是平常电脑可以看到的文件夹目录 版本库(Repository):存放git内容的文件夹例如: Git的版本库里存了很多东西 ...

  9. 计算机思维的逻辑基础是什么? & 计算思维

    l  计算机思维的逻辑基础: 计算机思维是指人们操作计算机时,计算机行使特定功能的运作方式. 逻辑基础则是指支撑事物运作的基本法则. 因而,计算机思维的逻辑基础可以理解为,计算机在行使特定功能时,其运 ...

  10. 钉钉小程序不用canvas在后端绘图前端用image标签获取图片的实践

    公司的需求要用电子员工卡代替用了N久的工作证,在各种场合刷二维码来代替刷卡.在钉钉小程序里实现.感觉这回又要躺坑里了. 钉钉小程序第一次做.我这个自封的GDI+大神才不要想用钉钉jsapi的方式用ca ...