牛客网-每日一练
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return int整型一维数组 # class Solution: def preorderTraversal(self , root ): res = [] def preorder(root): if root == None: return res.append(root.val) preorder(root.left) preorder(root.right) preorder(root) return res # write code here
求给定的二叉树的前序遍历。
例如:
给定的二叉树为{1,#,2,3},
返回:[1,2,3].
备注;用递归来解这道题很简单,你可以给出迭代的解法么