思路:先读入该Node, 然后是它的左节点,最后是右节点。所以,这是一个递归。
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param: root: A Tree
@return: Preorder in ArrayList which contains node values.
"""
def preorderTraversal(self, root):
# write your code here
self.list = []
def travel(root):
if root != None:
self.list.append(root.val)
travel(root.left)
travel(root.right)
else:
None
travel(root)
return self.list
网友评论