【LeetCode】【Python】Binary Tree Inorder Traversal
阿新 • • 發佈:2017-05-15
nod 不知道 otto div ack return integer neu else
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