【LeetCode】【Python】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
比較简单,就是转化成中序遍历就可以。訪问顺序是中序遍历左子树。根节点,中序遍历右子树
Python编程的时候须要注意,要在返回单一数字的时候加上中括号【】,否则Python不知道这是一个list
# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # @param root, a tree node # @return a list of integers def inorderTraversal(self, root): if root is None: return [] elif root.left is None and root.right is None: return [root.val] else: return self.inorderTraversal(root.left)+[root.val]+self.inorderTraversal(root.right)