【LeetCode with Python】 Flatten Binary Tree to Linked List
来源:互联网 发布:电脑照相机软件下载 编辑:程序博客网 时间:2024/05/01 06:47
博客域名:http://www.xnerv.wang
原题页面:https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/
题目类型:二叉树遍历,递归回溯
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/39453299
原题页面:https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/
题目类型:二叉树遍历,递归回溯
难度评价:★
本文地址:http://blog.csdn.net/nerv3x3/article/details/39453299
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
click to show hints.
Hints:
If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.
题目的算法本身并不难理解,按照先序遍历的顺序将一棵二叉树拉平为一个“链表”。因此后序遍历二叉树,先将左子树“拉平”,再将右子树“拉平”,然后如果左子树不是None,就将已经拉平为“链表”的左子树,嵌入到当前节点与直接右孩子节点(可能右孩子是None)之间。关键在于一些分支上要判断是否不为None,很容易出错。
class Solution: # @param root, a tree node # @return nothing, do it in place def flatten(self, root): if None == root: return if None != root.left: self.flatten(root.left) if None != root.right: self.flatten(root.right) left = root.left right = root.left while None != right and None != right.right: right = right.right if None != right: right.right = root.right if None != left: ### root.right = left root.left = None
0 0
- 【LeetCode with Python】 Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List Leetcode Python
- 【Leetcode】【python】Flatten Binary Tree to Linked List
- LeetCode 114. Flatten Binary Tree to Linked List(Python)
- LeetCode: Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode: Flatten Binary Tree to Linked List
- [Leetcode] Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- Leetcode: Flatten Binary Tree to Linked List
- leetcode Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- 【leetcode】Flatten Binary Tree to Linked List
- [LeetCode]Flatten Binary Tree to Linked List
- [Leetcode]Flatten Binary Tree to Linked List
- [leetcode]Flatten Binary Tree to Linked List
- LeetCode-Flatten Binary Tree to Linked List
- 【LeetCode with Python】 Count and Say
- 《炉石传说》架构设计赏析(3):Gameplay初探
- 【LeetCode with Python】 Divide Two Integers
- Android SDK目录详解
- 使用Firebug对js进行断点调试的图文方法
- 【LeetCode with Python】 Flatten Binary Tree to Linked List
- postgresql数据导入到mysql
- Android环境搭建之三 Eclipse下搭建Android + C/C++ + OpenCV 开发坏境
- c# 汉字转拼音
- 1~4组成无重复数字的三位数
- 短小精悍的虚拟机:JVM基本结构和功能介绍
- 【LeetCode with Python】 Insert Interval
- Android 命名规范 (提高代码可以读性)
- pageContext对象的用法