forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTreePreorderTraversal.java
51 lines (44 loc) · 1.27 KB
/
binaryTreePreorderTraversal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Source : https://oj.leetcode.com/problems/binary-tree-preorder-traversal/
// Inspired by : http://www.jiuzhang.com/solutions/binary-tree-preorder-traversal/
// Author : Lei Cao
// Date : 2015-10-05
/**********************************************************************************
*
* 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].
*
* Note: Recursive solution is trivial, could you do it iteratively?
*
**********************************************************************************/
package binaryTreePreorderTraversal;
import java.util.ArrayList;
import java.util.List;
/**
* Created by leicao on 5/10/15.
*/
public class binaryTreePreorderTraversal {
//Version 1: Traverse
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> results = new ArrayList<Integer>();
traversal(results, root);
return results;
}
private void traversal(List<Integer> results, TreeNode root) {
if (results == null || root == null) {
return;
}
results.add(root.val);
traversal(results, root.left);
traversal(results, root.right);
}
}