Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Binary Tree
- implement
- 자바
- Matrix
- database
- bit manipulation
- 파이썬
- hash table
- dynamic programming
- Class
- Math
- 구현
- two pointers
- sorting
- Stack
- string
- SQL
- geometry
- Counting
- array
- Binary Search
- simulation
- greedy
- Method
- Tree
- Data Structure
- java
- 코테
- 코딩테스트
- Number Theory
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 144. Binary Tree Preorder Traversal 본문
1. Input
1) TreeNode root
2. Output
1) root를 전위순회한 결과를 리스트에 담아 반환
3. Constraint
1) root에 있는 노드의 수는 [0, 100]이다.
2) -100 <= Node.val <= 100
4. Example
Input: root = [1,null,2,3] -> Output: [1,2,3]
5. Code
1) 첫 코드(2023/05/12)
/**
* 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;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> pre = new ArrayList<>();
if(root!=null){
pre.add(root.val);
pre.addAll(preorderTraversal(root.left));
pre.addAll(preorderTraversal(root.right));
}
return pre;
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 169. Majority Element (0) | 2023.05.13 |
---|---|
[LeetCode/Easy] 145. Binary Tree Postorder Traversal (0) | 2023.05.12 |
[LeetCode/Easy] 119. Pascal's Triangle II (0) | 2023.05.12 |
[LeetCode/Easy] 118. Pascal's Triangle (0) | 2023.05.12 |
[백준 온라인 저지] 24416. 알고리즘 수업 - 피보나치 수 1 (0) | 2023.05.12 |