↓快轉到主要內容

Binary-Tree

LeetCode 872: Leaf-Similar Trees
·1 分鐘
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 872 解題筆記:用 DFS 由左到右收集葉子;BFS 收集的順序是一層一層,不是由左到右
LeetCode 700: Search in a Binary Search Tree
·1 分鐘
LeetCode Easy Tree Binary-Search-Tree Binary-Tree
LeetCode 700 解題筆記:比目標大就往左、比目標小就往右,找到就回傳那個節點
LeetCode 671: Second Minimum Node In a Binary Tree
·1 分鐘
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 671 解題筆記:根就是最小值;第二小值是整棵樹裡第一個比根大的最小值,值等於根的子樹才需要往下找
LeetCode 637: Average of Levels in Binary Tree
·1 分鐘
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 637 解題筆記:層序遍歷,每層加總除以節點數
LeetCode 606: Construct String from Binary Tree
·1 分鐘
LeetCode Medium String Tree Depth-First-Search Binary-Tree
LeetCode 606 解題筆記:前序輸出,左子樹空但右子樹不空時要留一個空括號 (),右子樹空時可以省略
LeetCode 572: Subtree of Another Tree
·1 分鐘
LeetCode Easy Tree Depth-First-Search String-Matching Binary-Tree
LeetCode 572 解題筆記:對 root 的每一個節點都檢查一次「以它為根的子樹跟 subRoot 是否相同」;遞迴時要呼叫 isSubtree,不是只比左右小孩
LeetCode 563: Binary Tree Tilt
·1 分鐘
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 563 解題筆記:後序回傳子樹的總和,每個節點的 tilt 是左右子樹總和的差,加進答案
LeetCode 543: Diameter of Binary Tree
·1 分鐘
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 543 解題筆記:直徑可能不經過根;後序回傳高度,同時在每個節點用「左高 + 右高」更新答案;算的是邊數
LeetCode 501: Find Mode in Binary Search Tree
·1 分鐘
LeetCode Easy Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 501 解題筆記:BST 中序是排序的,相同的值會連在一起;中序時數目前這段的長度,比最大值大就重設答案,一樣大就加入
LeetCode 404: Sum of Left Leaves
·1 分鐘
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 404 解題筆記:DFS 時告訴小孩自己是不是左小孩,只有「是左小孩而且是葉子」才加
LeetCode 235: Lowest Common Ancestor of a Binary Search Tree
·1 分鐘
LeetCode Medium Tree Depth-First-Search Binary-Search-Tree Binary-Tree
LeetCode 235 解題筆記:p、q 都比節點小就往左,都比節點大就往右,否則(一邊一個或其中一個就是自己)這個節點就是 LCA
LeetCode 226: Invert Binary Tree
·1 分鐘
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 226 解題筆記:每個節點交換左右小孩;用同時指定,避免先改掉的那邊被拿來用