[Leetcode]@python 94. Binary Tree Inorder Traversal
2016-02-23 16:34
666 查看
题目链接
https://leetcode.com/problems/binary-tree-inorder-traversal/题目原文
Given a binary tree, return the inorder traversal of its nodes' values.For example:
Given binary tree
{1,#,2,3},
return
[1,3,2].
题目大意
中序遍历一棵二叉树解题思路
递归求解,先遍历左子树,再遍历根节点,最后遍历右子树代码
# 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 inorderTraversal_re(self, root, inorder): if not root: return inorder inorder = self.inorderTraversal_re(root.left, inorder) inorder.append(root.val) inorder = self.inorderTraversal_re(root.right, inorder) return inorder def inorderTraversal(self, root): """ :type root: TreeNode :rtype: List[int] """ return self.inorderTraversal_re(root, [])
相关文章推荐
- PyQt4 Python GUI窗体应用程序
- [Leetcode]@python 93. Restore IP Addresses
- python k-近邻算法
- Python练习-- slice
- Python练习-- loop
- Python练习-- list and tuple
- Python练习-- iterator
- Python练习-- condition
- [Leetcode]@python 92. Reverse Linked List II
- Python subprocess模块总结
- Python练习--character
- [解决] python WindowsError: [Error 3]
- Eclipse搭建Python开发环境+Python中文处理
- [Leetcode]@python 91. Decode Ways
- Python学习笔记_判断和循环
- Windows下Python开发环境搭建及 Python的HelloWorld示例
- python内置模块(四)
- python之文件编码转换
- 安装ipython import path error
- [备忘]Sublime Text 3 Python编辑环境搭建