题目要求

A binary tree is univalued if every node in the tree has the same value.

Return true if and only if the given tree is univalued.

题目分析及思路

题目要求当输入的二叉树的每个结点都有相同的值则返回true,否则返回false。可以使用队列保存每个节点,用val保存root节点的值。如果弹出的数字不等于val不等于root节点就立刻返回false。如果全部判断完成之后仍然没有返回false,说明所有的数字都等于root,返回true。

python代码​

# Definition for a binary tree node.

# class TreeNode:

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

class Solution:

def isUnivalTree(self, root):

"""

:type root: TreeNode

:rtype: bool

"""

q=collections.deque()

q.append(root)

val = root.val

while q:

node = q.popleft()

if not node:

continue

if val != node.val:

return False

q.append(node.left)

q.append(node.right)

return True

LeetCode 965 Univalued Binary Tree 解题报告的更多相关文章

  1. 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  2. LeetCode 965. Univalued Binary Tree

    A binary tree is univalued if every node in the tree has the same value. Return true if and only if ...

  3. LeetCode 226 Invert Binary Tree 解题报告

    题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...

  4. 【LeetCode】Balanced Binary Tree 解题报告

    [题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  5. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  6. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  7. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  8. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  9. 【Leetcode_easy】965. Univalued Binary Tree

    problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完

随机推荐

  1. [转]PowerDesigner大小写转换

    原文地址:https://blog.csdn.net/fzqlife/article/details/72769959?utm_source=blogxgwz7 在菜单栏找到:Tools-->E ...

  2. [转载][IoC容器Unity]第二回:Lifetime Managers生命周期

    1.引言 Unity的生命周期是注册的类型对象的生命周期,而Unity默认情况下会自动帮我们维护好这些对象的生命周期,我们也可以显示配置对象的生命周期,Unity将按照配置自动管理,非常方便,下面就介 ...

  3. 命令查看linux主机配置

    查看cpu: # 总核数 = 物理CPU个数 X 每颗物理CPU的核数 # 总逻辑CPU数 = 物理CPU个数 X 每颗物理CPU的核数 X 超线程数 # 查看物理CPU个数 cat /proc/cp ...

  4. CSS3 Flexbox可视化指南

    0. 目录 目录 引言 正文 1 引入 2 基础 3 使用 4 弹性容器Flex container属性 41 flex-direction 42 flex-wrap 43 flex-flow 44 ...

  5. (笔记)AT91SAM9260的启动过程详细解说

    Bootstrap的启动过程 一. 说明: Bootstrap启动代码是官方提供的一级启动代码,包括汇编和C语言两部分组成.对AT91SAM9260来说编译完成后,代码长度必须小于4KB,烧写到dat ...

  6. 关于spring boot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案

    搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了,但是我在实施过程中出现了这么一个问题,见下面,这里找到解决办法记录下来,供遇到同样的问题的同僚参考 Des ...

  7. 【Math】根据置信度、样本数相关推导过程

    时间长了会忘,备忘下. http://blog.csdn.net/liangzuojiayi/article/details/78044780 http://wiki.mbalib.com/wiki/ ...

  8. 两台centos之间传送文件

    https://www.cnblogs.com/pangguoming/p/9282762.html

  9. python -u 启动python文件的作用,PYTHONUNBUFFERED环境变量的作用

    python -u 启动python文件的作用是不缓存,直接把输出重定向到文件,比如nohup启动什么的,如果不使用-u启动,那么程序中的print和日志什么的,可能不会非常及时的重定向到out文件, ...

  10. Texture::setUnRefImageDataAfterApply

    当该值设置为true时,OSG在应用过该纹理对象后(apply),自动释放其对Image对象的引用,以减少内存占用