Given a binary tree, return the preorder traversal of its nodes' values.java
For example:
Given binary tree {1,#,2,3}
,
node
1 \ 2 / 3
return [1,2,3]
.spa
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<Integer> preorderTraversal(TreeNode root) { preOrder(root); return list; } public List<Integer> list = new ArrayList<Integer>(); public void preOrder(TreeNode root){ if(root == null)return; list.add(root.val); preOrder(root.left); preOrder(root.right); } }