LeetCode 114. Flatten Binary Tree to Linked List(Python)
来源:互联网 发布:网络大v颠倒是非 编辑:程序博客网 时间:2024/05/22 03:08
题目描述:
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
思路:
用列表保存该二叉树的前序遍历的结点,最后循环列表重新赋左右子树。
AC代码:
class Solution(object): def flatten(self, root): """ :type root: TreeNode :rtype: void Do not return anything, modify root in-place instead. """ ans = [] def preOrderTravel(root): if not root: return ans.append(root) preOrderTravel(root.left) preOrderTravel(root.right) preOrderTravel(root) copy = root for i in range(1, len(ans)): copy.left = None copy.right = ans[i] copy = ans[i]
阅读全文
0 0
- LeetCode 114. Flatten Binary Tree to Linked List(Python)
- 【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: 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
- Java:集合框架二(LinkerList)详解和代码示例
- 设计模式(13)享元模式--结构型
- 机器学习的关注点
- appium+python自动化测试
- 【C#】程序编写规范
- LeetCode 114. Flatten Binary Tree to Linked List(Python)
- 面向对象三大基本特性,五大基本原则
- bzoj 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛(BFS)
- 关于 Java 你不知道的十件事
- 利用angular指令监听ng-repeat渲染完成后执行脚本
- 脚本学习
- BZOJ2400: Spoj 839 Optimal Marks
- 【多校训练】hdu 6060 RXD and dividing dfs
- HDU 4609 3-idiots(多项式计数 + FFT卷积优化)