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
"""
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