mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 11:21:42 +08:00
add leetcode problem-cn part3
This commit is contained in:
62
算法题(国内版)/problem (Chinese)/树节点 [tree-node].html
Normal file
62
算法题(国内版)/problem (Chinese)/树节点 [tree-node].html
Normal file
@@ -0,0 +1,62 @@
|
||||
<p>给定一个表 <code>tree</code>,<strong>id</strong> 是树节点的编号, <strong>p_id</strong> 是它父节点的 <strong>id 。</strong></p>
|
||||
|
||||
<pre>+----+------+
|
||||
| id | p_id |
|
||||
+----+------+
|
||||
| 1 | null |
|
||||
| 2 | 1 |
|
||||
| 3 | 1 |
|
||||
| 4 | 2 |
|
||||
| 5 | 2 |
|
||||
+----+------+</pre>
|
||||
|
||||
<p>树中每个节点属于以下三种类型之一:</p>
|
||||
|
||||
<ul>
|
||||
<li>叶子:如果这个节点没有任何孩子节点。</li>
|
||||
<li>根:如果这个节点是整棵树的根,即没有父节点。</li>
|
||||
<li>内部节点:如果这个节点既不是叶子节点也不是根节点。</li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p>写一个查询语句,输出所有节点的编号和节点的类型,并将结果按照节点编号排序。上面样例的结果为:</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<pre>+----+------+
|
||||
| id | Type |
|
||||
+----+------+
|
||||
| 1 | Root |
|
||||
| 2 | Inner|
|
||||
| 3 | Leaf |
|
||||
| 4 | Leaf |
|
||||
| 5 | Leaf |
|
||||
+----+------+
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>解释</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>节点 '1' 是根节点,因为它的父节点是 NULL ,同时它有孩子节点 '2' 和 '3' 。</li>
|
||||
<li>节点 '2' 是内部节点,因为它有父节点 '1' ,也有孩子节点 '4' 和 '5' 。</li>
|
||||
<li>节点 '3', '4' 和 '5' 都是叶子节点,因为它们都有父节点同时没有孩子节点。</li>
|
||||
<li>样例中树的形态如下:
|
||||
<p> </p>
|
||||
|
||||
<pre> 1
|
||||
/ \
|
||||
2 3
|
||||
/ \
|
||||
4 5
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<p><strong>注意</strong></p>
|
||||
|
||||
<p>如果树中只有一个节点,你只需要输出它的根属性。</p>
|
Reference in New Issue
Block a user