码迷,mamicode.com
首页 > 编程语言 > 详细

【LeetCode】【Python】Binary Tree Inorder Traversal

时间:2017-05-15 10:07:43      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:efi   style   顺序   ram   否则   中序   and   lis   otto   

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

标签:efi   style   顺序   ram   否则   中序   and   lis   otto   

原文地址:http://www.cnblogs.com/ljbguanli/p/6854777.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!