-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL94.java
More file actions
28 lines (26 loc) · 914 Bytes
/
L94.java
File metadata and controls
28 lines (26 loc) · 914 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
import java.util.ArrayList;
import java.util.List;
class Solution94 {
class Solution {
/**
* 94. Binary Tree Inorder Traversal https://leetcode.com/problems/binary-tree-inorder-traversal/description/
* @timeComplexity O(n)
* @spaceComplexity O(1) ignoring stack space
* @param root Root of the binary tree
* @return List containing inorder traversed elements
*/
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorderHelper(root, result);
return result;
}
private void inorderHelper(TreeNode root, List<Integer> values) {
if (root == null) {
return;
}
inorderHelper(root.left, values);
values.add(root.val);
inorderHelper(root.right, values);
}
}
}