二叉树的递归遍历
递归三要素:确定递归函数的参数和返回值,确定终止条件,确定单层递归的逻辑
144. 二叉树的前序遍历
https://leetcode.cn/problems/binary-tree-preorder-traversal/description/
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
preorder(root,list);
return list;
}
public void preorder(TreeNode root , List<Integer> list){
if (root == null) return;
list.add(root.val);
preorder(root.left,list);
preorder(root.right,list);
}
标签:preorder,递归,随想录,list,第十四天,二叉树,root,List
From: https://www.cnblogs.com/jeasonGo/p/18082170