标签:traversal python 中序遍历 leetcode
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)
【LeetCode】【Python】Binary Tree Inorder Traversal,布布扣,bubuko.com
【LeetCode】【Python】Binary Tree Inorder Traversal
标签:traversal python 中序遍历 leetcode
原文地址:http://blog.csdn.net/monkeyduck/article/details/38108961