leetcode 114. Flatten Binary Tree to Linked List (Python版)
2016-01-23 22:37
411 查看
题目:
Given a binary tree, flatten it to a linked list in-place.
算法思路:
其实该题目就是二叉树前序遍历的变形
我代码沿用leetcode 144. Binary Tree Preorder Traversal
代码:
Given a binary tree, flatten it to a linked list in-place.
算法思路:
其实该题目就是二叉树前序遍历的变形
我代码沿用leetcode 144. Binary Tree Preorder Traversal
代码:
class Solution(object): def preorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ if root == None:return [] stack = [root] result = [] while len(stack) != 0: tmp_root = stack.pop() if tmp_root == None:continue result.append(tmp_root) stack.append(tmp_root.right) stack.append(tmp_root.left) return result def flatten(self, root): """ :type root: TreeNode :rtype: void Do not return anything, modify root in-place instead. """ result = self.preorderTraversal(root) for i in range(1,len(result)): result[i-1].left = None result[i-1].right = result[i]
相关文章推荐
- python day2 list tuple dict str
- python在windows的cmd中打印彩色文字
- Python Import 详解
- libpython2.7.a(abstract.o) recompile with -fPIC
- 《Python for kids》学习笔记(四)
- 在ubuntu 15.10 上配置vim ,用来进行python 开发
- python进阶——类和对象
- leetcode之insertion Sort list
- 【Python】Hello World 输入输出
- Python基础之day2
- Json概述以及python对json的相关操作
- Python字符串、元组、列表、字典互相转换的方法
- Python随手笔记第一篇(2)之初识列表和元组
- Python convert list string to list
- Python使用Requests第三方库自动登陆知乎
- eclipse环境搭建之三:Python
- Python collections.defaultdict() 与 dict的使用和区别
- 【python】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
- Python成长之路第一篇(2)-初识列表和元组
- python爬虫之赞同知乎用户所有答案以及给用户发私信