问题描述
https://leetcode.cn/problems/binary-tree-preorder-traversal/description/
解题思路
二叉树的先序遍历,没啥好说的。中-左-右。 先序中序后序 说的是中在哪里。
代码
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: res = [] def dfs(root): if root is None: return res.append(root.val) dfs(root.left) dfs(root.right) dfs(root) return res
标签:144,right,val,前序,dfs,二叉树,root,self,left From: https://www.cnblogs.com/bjfu-vth/p/17070982.html