LeetCode contest 177 5170. 验证二叉树

Table of Contents

中文版:

英文版:

My answer:

解题报告:


中文版:

二叉树上有 n 个节点,按从 0 到 n - 1 编号,其中节点 i 的两个子节点分别是 leftChild[i] 和 rightChild[i]。

只有 所有 节点能够形成且 只 形成 一颗 有效的二叉树时,返回 true;否则返回 false。

如果节点 i 没有左子节点,那么 leftChild[i] 就等于 -1。右子节点也符合该规则。

注意:节点没有值,本问题中仅仅使用节点编号。

 

示例 1:

输入:n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
输出:true
示例 2:

输入:n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
输出:false
示例 3:

输入:n = 2, leftChild = [1,0], rightChild = [-1,-1]
输出:false
示例 4:

输入:n = 6, leftChild = [1,-1,-1,4,-1,-1], rightChild = [2,-1,-1,5,-1,-1]
输出:false
 

提示:

1 <= n <= 10^4
leftChild.length == rightChild.length == n
-1 <= leftChild[i], rightChild[i] <= n - 1

英文版:

You have n binary tree nodes numbered from 0 to n - 1 where node i has two children leftChild[i] and rightChild[i], return true if and only if all the given nodes form exactly one valid binary tree.

If node i has no left child then leftChild[i] will equal -1, similarly for the right child.

Note that the nodes have no values and that we only use the node numbers in this problem.

 

Example 1:

Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,-1,-1,-1]
Output: true
Example 2:

Input: n = 4, leftChild = [1,-1,3,-1], rightChild = [2,3,-1,-1]
Output: false
Example 3:

Input: n = 2, leftChild = [1,0], rightChild = [-1,-1]
Output: false
Example 4:

Input: n = 6, leftChild = [1,-1,-1,4,-1,-1], rightChild = [2,-1,-1,5,-1,-1]
Output: false
 

Constraints:

1 <= n <= 10^4
leftChild.length == rightChild.length == n
-1 <= leftChild[i], rightChild[i] <= n - 1

My answer:

# 算法:只有根节点没有父节点,其余节点都只有一个父节点。

class Solution:
    def validateBinaryTreeNodes(self, n: int, leftChild: List[int], rightChild: List[int]) -> bool:
        parent_list = [0] * n
        for i in range(len(leftChild)):
            if leftChild[i] != -1:
                parent_list[leftChild[i]] += 1
            if rightChild[i] != -1:
                parent_list[rightChild[i]] += 1
        
        print(parent_list)
        num_zero = 0
        num_one = 0
        num_other = 0
        
        for x in parent_list:
            if x == 0:
                num_zero += 1
            elif x == 1:
                num_one += 1
            else:
                num_other += 1
        return num_zero == 1 and num_one == n-1 and num_other == 0  

代码优化后:

# 算法:只有根节点没有父节点,其余节点都只有一个父节点。

class Solution:
    def validateBinaryTreeNodes(self, n: int, leftChild: List[int], rightChild: List[int]) -> bool:
        parent_list = [0] * n
        for i in range(n):
            if leftChild[i] != -1:
                parent_list[leftChild[i]] += 1
            if rightChild[i] != -1:
                parent_list[rightChild[i]] += 1
        
        # print(parent_list)
        num_zero = 0
        num_one = 0
        # num_other = 0
        
        for x in parent_list:
            if x == 0:
                num_zero += 1
            elif x == 1:
                num_one += 1
            # else:
            #     num_other += 1
        # return num_zero == 1 and num_one == n-1 and num_other == 0  
        return num_zero == 1 and num_one == n-1
    

解题报告:

1、如何判断二叉树

只有一个根节点,只有根节点没有父节点,其余节点只有一个父节点。

2、parent_list 代表 n 个节点是否有父节点,下标 index 代表 n 个节点,parent_list[index] 代表 index 这个节点有几个父节点。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值