-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInOrderTraversalPrint.java
More file actions
44 lines (35 loc) · 1.13 KB
/
Copy pathInOrderTraversalPrint.java
File metadata and controls
44 lines (35 loc) · 1.13 KB
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.*;
class Solution {
// In order traversals are very easy, all you need to make
// sure is that the recursion fairy will take care of everything
//
// imagine that the recursion fairy has done the traversal of
// adding in the left tree and the traversal of adding in the right
// tree
//
// all you need to do is the final step which is just adding the
// root to the linked list
//
// another thing to take note of is that the return valeu for the
// inOrder traversal needs to be a list itself
List<Integer> inorderList = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root == null) {
return inorderList;
} else {
inorderTraversal(root.left);
inorderList.add(root.val);
inorderTraversal(root.right);
}
return inorderList;
}
}