File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Lintcode/src/chapter3_binary_tree_and_divide_and_conquer Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ package chapter3_binary_tree_and_divide_and_conquer ;
2
+
3
+ import java .util .ArrayList ;
4
+
5
+ import classes .TreeNode ;
6
+
7
+ public class BinaryTreePreorderTraversal {
8
+
9
+ /**
10
+ * @param root: The root of binary tree.
11
+ * @return: Preorder in ArrayList which contains node values.
12
+ */
13
+
14
+ //easily 1 min AC! Cheers!
15
+ public ArrayList <Integer > preorderTraversal (TreeNode root ) {
16
+ // write your code here
17
+ ArrayList <Integer > list = new ArrayList <Integer >();
18
+ dfs (root , list );
19
+ return list ;
20
+ }
21
+
22
+ void dfs (TreeNode root , ArrayList <Integer > list ){
23
+ if (root == null ) return ;
24
+ list .add (root .val );
25
+ dfs (root .left , list );
26
+ dfs (root .right , list );
27
+ return ;
28
+ }
29
+
30
+ }
You can’t perform that action at this time.
0 commit comments