-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTInOrderTraversal.java
37 lines (36 loc) · 1020 Bytes
/
BTInOrderTraversal.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
/*
Given the root of a binary tree, return the inorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3]
Output: [1,3,2]
*/
import java.util.ArrayList;
import java.util.List;
public class BTInOrderTraversal {
public static void main(String[] args) {
TreeNode root=new TreeNode(1);
root.left=null;
root.right=new TreeNode(2);
root.right.left=new TreeNode(3);
List<Integer> ans=inorderTraversal(root);
System.out.println(ans);
}
public static List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result=new ArrayList<>();
if(root==null){
return result;
}
result.addAll(inorderTraversal(root.left));
result.add(root.data);
result.addAll(inorderTraversal(root.right));
return result;
}
}
class TreeNode{
int data;
TreeNode left,right;
public TreeNode(int key){
key=data;
left=right=null;
}
}