-
LeetCode - Binary Tree Inorder Traversal [Java]Algorithms/- LeetCode 2022. 2. 7. 22:56
- 문제 링크 : Binary Tree Inorder Traversal
Binary Tree Inorder Traversal - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
정답(Solution)
Stack 풀이
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ import java.util.*; class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> answer = new ArrayList<>(); Stack<TreeNode> stack = new Stack(); while(root != null || !stack.isEmpty()){ while(root != null){ stack.push(root); root = root.left; } root = stack.pop(); answer.add(root.val); root = root.right; } return answer; } }
재귀 풀이
import java.util.*; class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> iList = new ArrayList<>(); dfs(root, iList); return iList; } private void dfs(TreeNode root, List<Integer> iList){ if(root == null){ return; } dfs(root.left, iList); iList.add(root.val); dfs(root.right, iList); } }
분석
- DFS
- 가장 기본적인 개념의 풀이
반응형