အားလုံးပဲ မင်္ဂလာပါ။ ဒီနေ့ Day 8 အတွက်ကတော့ Leetcode ရဲ့ difficulties Medium level problem တစ်ပုဒ်ဖြစ်တဲ့ 608. Tree Node ဆိုတဲ့ problem ကို ရွေးချယ်ထားပါတယ်။ ဒီပုစ္ဆာ အတွက်ကတော့ Tree table တစ်ခုထဲပဲပေးထားပါတယ်။ ဒီ table မှာဆို id (node id) နဲ့ p_id (parent node id) ဆိုပြီးနှစ်ခုပေးထားပါတယ်။ node တစ်ခုချင်းစီက Root, Inner, Leaf ဆိုတဲ့ type သုံးမျိုးထဲက တစ်မျိုးဖြစ်ရပါမယ်။ လိုချင်တာကတော့ node id ရယ် node type ရယ်ကို output အနေနဲ့ ထုတ်ပေးရမှာဖြစ်ပါတယ်။

1
2Input:
3Tree table:
4+----+------+
5| id | p_id |
6+----+------+
7| 1 | null |
8| 2 | 1 |
9| 3 | 1 |
10| 4 | 2 |
11| 5 | 2 |
12+----+------+
13
14Output:
15+----+-------+
16| id | type |
17+----+-------+
18| 1 | Root |
19| 2 | Inner |
20| 3 | Leaf |
21| 4 | Leaf |
22| 5 | Leaf |
23+----+-------+Explanation
- Node 1 is the root node because its parent node is null and it has child nodes 2 and 3.
- Node 2 is an inner node because it has parent node 1 and child node 4 and 5.
- Nodes 3, 4, and 5 are leaf nodes because they have parent nodes and they do not have child nodes.
Example ပြထားတာမှာဆိုရင် ပုံလေးနဲ့ကြည့်ရင်ပိုပြီးမြင်သာမှာဖြစ်ပါတယ်။ ပထမဆုံးအနေနဲ့ id 1 က Root node ဖြစ်ပါတယ်။ ဘာလို့လည်းဆိုတော့ parent id က null ဖြစ်ပြီး child တွေလည်းရှိနေလို့ပါ။ id 2 ကတော့ inner node ဖြစ်ပါတယ်။ parent id လည်းရှိနေပြီး child node တွေလည်းရှိနေလို့ပဲဖြစ်ပါတယ်။ နောက်ဆုံးအနေနဲ့ parent id ရှိပြီး child id မရှ်ရင် Leaf node ပဲဖြစ်ပါတယ်။
အဲ့တော့ note အနေနဲ့ဆို
- Root - parent id null and has child node
- Inner - parent is not null and has child node
- Leaf - parent id is not null and no child node
ပဲဖြစ်ပါတယ်။ အဲ့တာဆို ကျွန်တော်တို့ရေးကြည့်လိုက်ရအောင်ဗျာ။
1
2SELECT t.id ,
3 (CASE
4 WHEN t.p_id IS NULL THEN 'Root'
5 WHEN t.id IN (SELECT t2.p_id FROM Tree t2 WHERE t2.p_id IS NOT NULL) THEN 'Inner'
6 ELSE 'Leaf'
7 END
8 )
9 AS 'type' FROM Tree t;ဒီမှာဆိုကျွန်တော်တို့က condition 3 ခုထဲက တစ်ခုချင်းစီစစ်ပေးဖို့လိုတော့ Case သုံးထားတာပဲဖြစ်ပါတယ်။ ပထမဆုံး အနေနဲ့ က Root type အတွက်က note ထဲကအတိုင်း parent id null ဖြစ်နေတာကို root type အနေနဲ့ သတ်မှတ်ထားလိုက်ပါတယ်။ Inner node အတွက်ကတော့ ပထမတစ်ကြောင်းမှာ parent id null ဖြစ်ရင် Root ဆိုပြီးသတ်မှတ်ထားတော့ သေချာတာက parent id က null မဖြစ်တော့ပါဘူး။ Inner node ဖြစ်ဖို့ဆို child node တွေပါရှိဖို့လိုပါတယ်။ ဒါကြောင့် sub query လေးနဲ့ parent id တွေကိုရှာပြီး အဲ့ထဲမှာပါနေခဲ့ရင် inner node ဖြစ်ပြီပေါ့ဗျာ။ နောက်ဆုံးမှာတော့ parent id ရှိပြီး child node မရှိတဲ့ node တွေပဲကျန်တော့တာဆိုတော့ Leaf node အနေနဲ့ သတ်မှတ်ထားလိုက်ပြီပဲဖြစ်ပါတယ်။
ဒါဆို Day 8 အတွက်လာ်းအဆင်ပြေသွားခဲ့ပြီဗျာ။ Video ကိုတော့အောက်က YouTube link လေးကနေတစ်ဆင့်ဝင်ကြည့်နိုင်ပါတယ်ဗျာ။ Stay safe and see you allပါဗျာ။
30 Days Of LeetCode Database Challenge Day 8 - (608. Tree Node)
Watch on YouTubeDiscussion
Join the conversation
How do you feel about this article?
Comments
Sign in to join the conversation
Sign in to be the first to comment!
Share Your Article
Share with your professional network
Recent Articles

AWS - Application Load Balancer
Elastic Load Balancing (ELB) ELB ဆိုတာကတော့ request တွေကို တစ်နေရာတည်းမှ လက်ခံကာ Amazon EC2 instances၊ containers, etc.....

Terraform Day 3: Benefits of Terraform State
Terraform ကိုလေ့လာ တဲ့အခါ ကျွန််တော်တို့ရဲ့ Project Folder ထဲမှာ terraform.tfstate ဆိုတဲ့ ဖိုင်လေးကို တွေ့ဖူးကြပါလိမ့်မယ...

Terraform Day 2: Essential IaC Principles You Must Know
မနေ့ကတော့ Terraform အကြောင်း အကြမ်းဖျင်း Concept ကို ပြောပြခဲ့ပြီးပြီဆိုတော့ ဒီနေ့မှာတော့ Terraform ကို Professional ကျက...

TCP/IP Protocol
အားလုံးပဲမင်္ဂလာပါ။ ဒီနေ့ ကျွန်တော်တို့ TCP/IP Protocol အကြောင်း ဆွေးနွေးသွားပါမယ်။ ပထမဆုံးအနေနဲ့ TCP/IP ရဲ့ History လေး...

Terraform Day 1: Introduction to IAC and Terraform
ကျွန်တော်တို့ cloud အကြောင်း စပြောကြပြီဆိုရင် အရင်ဆုံး ခေါင်းထဲရောက်လာတာ Console ထဲဝင်၊ UI ကနေ ခလုတ်လေးတွေ လိုက်နှိပ်ပြီ...


