일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- assignment
- palindrome
- impl
- pulldown
- 풀다운저항
- NotFoundError
- atmega128
- KNN
- 풀업저항
- pullup
- leetcode
- 회로
- #9
- softmax backpropagation
- autoencoder
- Softmax
- error
- cs231n
- pyTorch
- assignment1
- Floating
- Features
- backward pass
- Big size image
- neural net
- Solution
- Backpropagation
- Circuit
- two-layer neural net
- TensorFlow
- Today
- Total
코딩공부
LeetCode 101. Symmetric Tree 본문
Binary Tree가 symmetric한지 알아내는 문제이다.
https://leetcode.com/problems/symmetric-tree/
Symmetric Tree - LeetCode
Can you solve this real interview question? Symmetric Tree - Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center). Example 1: [https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg] Input: roo
leetcode.com
Follow up에서 recursive, iterative solution 각각으로 풀어보는것을 권장한다.
1. Recursive Solution
Tree형태이므로, 왼쪽과 오른쪽의 node를 비교하며 찾는다.
if l and r 구문의 경우, l 과 r 중 하나라도 None일 때 node.left, node.right 에서 발생할 수 있는 error를 처리하고자 적용되었다.
else에서 l == r인 이유는 NoneType에는 .val 값이 존재하지 않고 둘다 같은 None인지만 알아내면 되기 때문이다.
2. Iterative Solution
Iterative Solution의 경우, 코드가 좀더 복잡하다.
stack을 사용하여 DFS방식의 iterative solution을 만들었다.
보다 효율적으로 푸는 방법으로는 하나의 stack을 활용하여 memory 사용량을 줄일 수 있다.(권장)
Queue를 사용해도 똑같은 방법으로 풀 수 있다.
차이점은 FIFO를 구현하기 위해 pop(0)을 한 것 뿐이다.
질문이나 지적은 댓글로..
'ML , DL (2019) > 알고리즘' 카테고리의 다른 글
LeetCode 9. Palindrome Number (0) | 2023.08.17 |
---|---|
LeetCode 160. Intersection of Two Linked Lists (0) | 2023.08.02 |