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 | 31 |
Tags
- serverless
- lambda
- IaC
- 분산시스템
- aws
- amazonqcli
- sns
- Validation
- Lamda
- SageMaker
- cloudwatch
- CHECK
- rds
- 병목
- PACELC
- kubernetes
- fcm
- CAP
- terraform
Archives
- Today
- Total
잡다한 IT 지식
404. Sum of Left Leaves 본문
본 알고리즘 풀이는 Routine Study에서 진행하고 있습니다.
저를 포함한 구성원이 대부분 초보이므로, 원하시는분은 언제라도 들어오셔도 좋습니다.
문의는 댓글 바람.
Sum of Left Leaves - 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
[문제 설명]

왼쪽 leaf에 같의 총합.
[자바코드]
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root.left == null && root.right == null) return 0;
int answer = 0;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode temp = queue.poll();
if (temp.left != null) {
queue.offer(temp.left);
if (temp.left.left == null && temp.left.right == null) answer += temp.left.val;
}
if (temp.right != null) queue.offer(temp.right);
}
return answer;
}
}
[코틀린코드]
class Solution {
fun sumOfLeftLeaves(root: TreeNode?): Int {
if (root?.left == null && root?.right == null ) return 0
var answer = 0
val queue: Queue<TreeNode> = LinkedList<TreeNode>()
queue.offer(root)
while (!queue.isEmpty()) {
val node = queue.poll()
node.left?.also { left ->
queue.offer(left)
if (left.left == null && left.right == null) answer += left.`val`
}
node.right?.also { right -> queue.offer(right) }
}
return answer
}
}'알고리즘 문제 풀이 > BFS' 카테고리의 다른 글
| 백준 - 나이트의 이동 (0) | 2022.01.04 |
|---|---|
| 백준 - 촌수 계산 (0) | 2022.01.02 |
| 463. Island Perimeter (0) | 2021.09.01 |
| 965. Univalued Binary Tree (0) | 2021.09.01 |
| 993. Cousins in Binary Tree (0) | 2021.08.27 |