mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
52 lines
2.2 KiB
HTML
52 lines
2.2 KiB
HTML
<p>二叉树上有 <code>n</code> 个节点,按从 <code>0</code> 到 <code>n - 1</code> 编号,其中节点 <code>i</code> 的两个子节点分别是 <code>leftChild[i]</code> 和 <code>rightChild[i]</code>。</p>
|
||
|
||
<p>只有 <strong>所有</strong> 节点能够形成且 <strong>只</strong> 形成 <strong>一颗</strong> 有效的二叉树时,返回 <code>true</code>;否则返回 <code>false</code>。</p>
|
||
|
||
<p>如果节点 <code>i</code> 没有左子节点,那么 <code>leftChild[i]</code> 就等于 <code>-1</code>。右子节点也符合该规则。</p>
|
||
|
||
<p>注意:节点没有值,本问题中仅仅使用节点编号。</p>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>示例 1:</strong></p>
|
||
|
||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/23/1503_ex1.png" style="height: 287px; width: 195px;"></strong></p>
|
||
|
||
<pre><strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
|
||
<strong>输出:</strong>true
|
||
</pre>
|
||
|
||
<p><strong>示例 2:</strong></p>
|
||
|
||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/23/1503_ex2.png" style="height: 272px; width: 183px;"></strong></p>
|
||
|
||
<pre><strong>输入:</strong>n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
|
||
<strong>输出:</strong>false
|
||
</pre>
|
||
|
||
<p><strong>示例 3:</strong></p>
|
||
|
||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/23/1503_ex3.png" style="height: 174px; width: 82px;"></strong></p>
|
||
|
||
<pre><strong>输入:</strong>n = 2, leftChild = [1,0], rightChild = [-1,-1]
|
||
<strong>输出:</strong>false
|
||
</pre>
|
||
|
||
<p><strong>示例 4:</strong></p>
|
||
|
||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/23/1503_ex4.png" style="height: 191px; width: 470px;"></strong></p>
|
||
|
||
<pre><strong>输入:</strong>n = 6, leftChild = [1,-1,-1,4,-1,-1], rightChild = [2,-1,-1,5,-1,-1]
|
||
<strong>输出:</strong>false
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>1 <= n <= 10^4</code></li>
|
||
<li><code>leftChild.length == rightChild.length == n</code></li>
|
||
<li><code>-1 <= leftChild[i], rightChild[i] <= n - 1</code></li>
|
||
</ul>
|