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 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 解题报告的更多相关文章
- 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 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 ...
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
- 【LeetCode】Balanced Binary Tree 解题报告
[题目] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【Leetcode_easy】965. Univalued Binary Tree
problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完
随机推荐
- windows_硬盘上设置虚拟内存
1)在桌面上的“计算机”或“我的电脑”上右键->属性->高级->性能->设置->高级->虚拟内存->更改. 2)在虚拟内存更改页面,先选择在哪个磁盘上设置虚拟 ...
- MobaXterm 加装cygwin软件包
上次在<MobaXterm: SSH/X远程客户端, Xmanager的最佳免费替代品>里面介绍了MobaXterm这个Windows上的便携 多合一unix工具箱,它基于Cygwin,集 ...
- 【iCore4 双核心板_ARM】例程二十二:LWIP_UDP实验——以太网数据传输
实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...
- Java编程的逻辑 (82) - 理解ThreadLocal
本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...
- 聊聊Python中的生成器和迭代器
Python中有两个重要的概念,生成器和迭代器,这里详细记录一下. 1. 生成器 什么是生成器呢? 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包 ...
- (个人)Zookeeper集群环境部署
一.准备工作 1. 下载zookeeper,下载地址:https://zookeeper.apache.org/releases.html#download 2. 下载CentOS7的镜像,下载地址 ...
- java-信息安全(十二)-数字签名【Java证书体系实现】
概述 信息安全基本概念 数字证书 数字证书就是互联网通讯中标志通讯各方身份信息的一串数字,提供了一种在Internet上验证通信实体身份的方式,数字证书不是数字身份证,而是身份认证机构盖在数字身份证上 ...
- laravel项目composer安装
1.下载 Composer (https://pkg.phpcomposer.com/#how-to-install-composer) 安装前请务必确保已经正确安装了 PHP.打开命令行窗口并执行 ...
- Python终端自动补全
在-目录下添加一个文件,名字为.pythonstartup.py #!/usr/bin/python # -*- coding: UTF-8 -*- import readline, rlcomple ...
- 【转帖】oracle数据类型和对应的java类型
原文地址:http://otndnld.oracle.co.jp/document/products/oracle10g/102/doc_cd/java.102/B19275-03/datacc.ht ...