二叉树的前序便利
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3]
1
2
/
3
输出: [1,2,3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
import java.util.ArrayList; class Solution { public List<Integer> preorderTraversal(TreeNode root) { ArrayList<Integer> res=new ArrayList<Integer>(); if(root==null)return res; order(root,res); return res; } private static void order(TreeNode root, ArrayList<Integer> list){ if(root != null){ list.add(root.val); order(root.left,list); order(root.right,list); } } }
|
近期评论