这题就是二叉树的前序遍历。代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
a = []
def preorder(root):
if root == None:
return []
a.append(root.val)
if root.left != None:
preorder(root.left)
if root.right != None:
preorder(root.right)
preorder(root)
return a