Created
May 29, 2014 01:01
-
-
Save Cee/ee32c4f68c135e2b9f35 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) { | |
List<Integer> ret = new ArrayList<Integer>(); | |
if (root != null){ | |
ret.add(root.val); | |
if (root.left != null) ret.addAll(preorderTraversal(root.left)); | |
if (root.right != null) ret.addAll(preorderTraversal(root.right)); | |
} | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment