728x90
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> answer = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root == null) {
return answer;
}
TreeNode current = root;
while(current != null || !stack.isEmpty()) {
while (current != null) {
stack.push(current);
current = current.left;
}
current = stack.pop();
answer.add(current.val);
current = current.right;
}
return answer;
}
}
Inorder Traversal은
Left, Visit, Right의 순서로 기억하면 된다.
728x90
'알고리즘' 카테고리의 다른 글
[LeetCode] 145. Binary Tree Postorder Traversal (0) | 2023.12.06 |
---|---|
[LeetCode] 144. Binary Tree Preorder Traversal (0) | 2023.12.05 |
[LeetCode] 111. Minimum Depth of Binary Tree (0) | 2023.12.05 |
[LeetCode] 110. Balanced Binary Tree (0) | 2023.12.05 |
[LeetCode] 104. Maximum Depth of Binary Tree (0) | 2023.12.05 |