首页 > 代码库 > Binary Tree Preorder Traversal

Binary Tree Preorder Traversal

第一次看到accept,心里有点小开心

/**
* Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { void show(List<Integer> li,TreeNode p){ if(p!=null){ li.add(p.val); show(li,p.left); show(li,p.right); } } public List<Integer> preorderTraversal(TreeNode root) { List<Integer> li = new ArrayList<Integer>(); show(li,root); return li; } }