题解 | #判断是不是完全二叉树#

判断是不是完全二叉树

https://www.nowcoder.com/practice/8daa4dff9e36409abba2adbe413d6fae

from re import T
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param root TreeNode类 
# @return bool布尔型
#
class Solution:
    def isCompleteTree(self , root: TreeNode) -> bool:
        # write code here
        if root is None:
            return False
        qe = [root]
        flag = True
        while len(qe)>0:
            this = qe.pop(0)
            if this.left is not None and this.right is not None:
                if flag==False:
                    return False
                qe.append(this.left)
                qe.append(this.right)
            elif this.left is not None and this.right is None:
                if flag==False:
                    return False
                flag = False
                qe.append(this.left)
            elif this.right is not None and this.left is None:
                return False
            else:
                flag = False
        
        return True

全部评论

相关推荐

点赞 评论 收藏
分享
10-07 23:57
已编辑
电子科技大学 Java
八街九陌:博士?客户端?开发?啊?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务