Binary Tree Preorder Traversal python

来源:互联网 发布:网络摄像机ip设置 编辑:程序博客网 时间:2024/05/18 02:43

Binary Tree Preorder Traversal 
"""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        if root is None:            return []        arrLeft  = self.preorderTraversal(root.left)        arrRight = self.preorderTraversal(root.right)        return [root.val] + arrLeft + arrRight