[LeetCode]:144:Binary Tree Preorder Traversal
题目:
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,2,3]
.
代码:
public class Solution { public static ArrayList<Integer> listResult = new ArrayList<Integer>(); public List<Integer> preorderTraversal(TreeNode root) { listResult.clear(); if(root!=null){ getNode(root); } return listResult; } public static void getNode(TreeNode root){ if(root == null){ return; } listResult.add(root.val); getNode(root.left); getNode(root.right); } }