Solved on 2024/02/21
Solved 2nd on 2025/04/27
題目
https://leetcode.com/problems/binary-tree-right-side-view/description/
思路
使用DFS,只記錄最右邊的node (WA)
class Solution {
JustinLiu changed 2 months agoView mode Like Bookmark
題目
https://leetcode.com/problems/binary-tree-level-order-traversal/description/
思路
這個解法其實就是用簡單的BFS來完成level order traversal.
在每次pop之前都先去計算目前的queue有多少個element
如此一來我們就可以知道在這次新增了多少個element
/**
* Definition for a binary tree node.
JustinLiu changed 2 months agoView mode Like Bookmark
題目
https://leetcode.com/problems/subtree-of-another-tree/description/
思路
這個解法主要是把問題拆為兩個步驟:第一個步驟是先去traverse這顆tree,直到他們的root是相同的
第二個步驟是當他們的頭相同後,我們再去比較他們的子樹是否相同
如果不相同的話,我們就去判斷root的子樹有沒有可能會成功這樣~
/**
* Definition for a binary tree node.
JustinLiu changed 2 months agoView mode Like Bookmark
2023/01/21 Daily Challenge
2025/04/05 Second solved by Neetcode150
題目
https://leetcode.com/problems/house-robber/description/?envType=daily-question&envId=2024-01-23
思路
這個解法....
class Solution {
JustinLiu changed 3 months agoView mode Like Bookmark
Blind 169 Challenge Solved on 2024/02/13
Second solved on Neetcode150 on 2025/02/26
題目
https://leetcode.com/problems/rotting-oranges/description/
思路
先說這個解法看似正確但答案會是錯的
首先看到這個題目就應該要想到要使用BFS來做擴散,因為他會是levelOrder的去計算level
所以要先造訪grid上的每一個(x,y)來去計算Rotten orange的位置,在這裡我使用pair來儲存x,y,就不需要另外設計資料結構。
JustinLiu changed 4 months agoView mode Like Bookmark
First solved on 2023/12/16
Second solved on 2025/01/04
題目
https://leetcode.com/problems/invert-binary-tree/description/
思路
這個題目用簡單的DFS搭配交換左子樹跟右子樹即可。
class Solution {
JustinLiu changed 5 months agoView mode Like Bookmark
First Solved on 2023/12/29
second solved on 2025/01/04
題目
https://leetcode.com/problems/symmetric-tree/
思路
這個解法其實不太算是我想的,但我覺得這個寫法很讚
最主要就是在dfs裡面去判斷兩個node是否相同,如果相同的話就繼續往下比....跟一般的DFS差在他用兩個ptr去指
JustinLiu changed 6 months agoView mode Like Bookmark