Tree Node
透過leetcode 608
Tree Node來練習
Learn More →
id 是該表的主鍵列。
該表的每一行都包含有關節點的 id 及其在樹中的父節點的 id 的信息。
給定的結構總是一棵有效的樹。
樹中的每個節點都可以是以下三種類型之一:
"Leaf":如果節點是葉子(最下面的點)節點。
"Root":如果節點是樹的根(最上面的點)。
"Inner":如果該節點既不是葉節點也不是根節點(中間 不是最上面也不是最下面)。
編寫一個 SQL 查詢來報告樹中每個節點的類型。
返回按id升序排列的結果表。
查詢結果格式如下例所示。
Learn More →
以下圖解為第一個例題
1.
SELECT P_ID FROM Table_1
Learn More →
2.
SELECT * FROM Table_1 WHERE ID IN (SELECT P_ID FROM Table_1)
Learn More →
3.
select id,
case when p_id is null then 'Root'
when id in (select p_id from tree) then 'Inner'
else 'Leaf'
end as Type
FROM tree
以下圖解為第二個例題
1.
select * from Table_1 M
left outer join Table_1 A on M.id = A.p_id
Learn More →
2.
select distinct M.id,
case
when M.p_id is null then 'Root'
when A.p_id is not null then 'Inner'
else 'Leaf'
end as 'type' from Tree M
left outer join Tree A on M.id = A.p_id
最小生成樹(Minimum Spanning Tree,MST)是指在一個帶權無向圖中,找到一棵包含所有節點,權值最小的樹。其中,權值是指樹中所有邊權重的總和。
May 9, 2025運算式(Expression)有三種表示方式:中序式(Infix)、前序式(Prefix)、後序式(Postfix)
Dec 27, 2024Pinia簡介
Dec 26, 2024氣泡排序是反覆進行將相鄰數字做比較後重新排序,因排序時一個一個浮出序列頂部,很像水中泡泡浮起來的樣子,亦稱泡泡排序,最壞情況下,數是由大排到小,每次比較後將數值對調,因此,時間複雜度為O(n^2)。
Dec 24, 2024or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up