-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBinaryTreeInorderTraversal.java
More file actions
36 lines (25 loc) · 933 Bytes
/
BinaryTreeInorderTraversal.java
File metadata and controls
36 lines (25 loc) · 933 Bytes
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
// Time Complexity O(n) Space Complexity O(n)
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
// Storing Result In A List
List<Integer> treeList = new LinkedList<>();
// Calling In Order Function
inorder(treeList, root);
// Returning Final Result
return treeList;
}
// Inorder Function
public void inorder(List<Integer> list, TreeNode root){
// In case The Root Is Null We Stop The Recursive Call
// This Is The Base Condition
if(root == null){
return;
}
// We Traverse Through All The Left Nodes First
inorder(list, root.left);
// Then We Add The Values
list.add(root.val);
// And Finally We Traverse The Right Nodes Of The Tree.
inorder(list, root.right);
}
}