-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0094_Binary_Tree_Inorder_Traversal.java
35 lines (30 loc) · 1.13 KB
/
0094_Binary_Tree_Inorder_Traversal.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
/*
* 94. Binary Tree Inorder Traversal
* Problem Link: https://leetcode.com/problems/binary-tree-inorder-traversal/
* Difficulty: Easy
*
* Solution Created by: Muhammad Khuzaima Umair
* LeetCode : https://leetcode.com/mkhuzaima/
* Github : https://github.com/mkhuzaima
* LinkedIn : https://www.linkedin.com/in/mkhuzaima/
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
// create a list to store the result of the inorder traversal
List<Integer> list = new ArrayList<>();
// call the helper function to perform the traversal
inorderHelper(list, root);
// return the resulting list
return list;
}
private void inorderHelper(List<Integer> list, TreeNode root) {
// base case: if the root is null, there's nothing to traverse
if (root == null) return;
// recursively traverse the left subtree
inorderHelper(list, root.left);
// add the root node's value to the list
list.add(root.val);
// recursively traverse the right subtree
inorderHelper(list, root.right);
}
}