Skip to content

Instantly share code, notes, and snippets.

@honux77
Created June 28, 2014 01:43
Show Gist options
  • Select an option

  • Save honux77/c710411c12fda70a0ae6 to your computer and use it in GitHub Desktop.

Select an option

Save honux77/c710411c12fda70a0ae6 to your computer and use it in GitHub Desktop.
/**
* 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> l = new LinkedList<Integer>();
_preorderTraversal(root, (LinkedList) l);
return l;
}
private void _preorderTraversal(TreeNode root, LinkedList<Integer> list) {
if(root == null)
return;
list.add(root.val);
_preorderTraversal(root.left, list);
_preorderTraversal(root.right, list);
}
}
@honux77
Copy link
Copy Markdown
Author

honux77 commented Jun 28, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment