【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/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.
Example 1:
Input: [1,1,1,1,1,null,1]
Output: true
Example 2:
Input: [2,2,2,5,2]
Output: false
Note:
- The number of nodes in the given tree will be in the range [1, 100].
- Each node’s value will be an integer in the range [0, 99].
题目大意
问二叉树的每个节点的值是不是都是一样的。
解题方法
BFS
可以使用BFS或者DFS.这个题我直接花了3分钟写了个简单版本的BFS就能通过了。使用队列保存每个节点,用val保存root节点的值。如果弹出的数字不等于val不等于root节点就立刻返回false。如果全部判断完成之后仍然没有返回false,说明所有的数字都等于root,返回true.
python代码如下:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
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
C++代码如下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isUnivalTree(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
int val = root->val;
while (!q.empty()) {
TreeNode* node = q.front(); q.pop();
if (!node) continue;
if (node->val != val)
return false;
q.push(node->left);
q.push(node->right);
}
return true;
}
};
DFS
DFS代码很简单,我就不解释了。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isUnivalTree(TreeNode* root) {
return dfs(root, root->val);
}
bool dfs(TreeNode* root, int val) {
if (!root) return true;
if (root->val != val) return false;
return dfs(root->left, val) && dfs(root->right, val);
}
};
日期
2018 年 12 月 30 日 —— 周赛差强人意
【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)的更多相关文章
- LeetCode 965 Univalued Binary Tree 解题报告
题目要求 A binary tree is univalued if every node in the tree has the same value. Return true if and onl ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 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 ...
随机推荐
- excel-大于0的数值标记红色且标记红色上箭头,小于0的数值标记绿色且标记绿色下箭头,等于0的数值标记黄色且标记右箭头
0.数值是常规的数值: [蓝色]"↑"0;[红色]"↓"0;[黄色]"→"0 [蓝色]"↑"0.0;[红色]" ...
- 漏洞分析:CVE-2017-17215
漏洞分析:CVE-2017-17215 华为HG532路由器的命令注入漏洞,存在于UPnP模块中. 漏洞分析 什么是UPnP? 搭建好环境(使用IoT-vulhub的docker环境),启动环境,查看 ...
- 自动化测试系列(三)|UI测试
UI 测试是一种测试类型,也称为用户界面测试,通过该测试,我们检查应用程序的界面是否工作正常或是否存在任何妨碍用户行为且不符合书面规格的 BUG.了解用户将如何在用户和网站之间进行交互以执行 UI 测 ...
- C#表格,表格信息、GridView使用。
page: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="test1.a ...
- LInkedList总结及部分底层源码分析
LInkedList总结及部分底层源码分析 1. LinkedList的实现与继承关系 继承:AbstractSequentialList 抽象类 实现:List 接口 实现:Deque 接口 实现: ...
- ctfshow WEB入门 信息收集 1-20
web1 题目:开发注释未及时删除 查看页面源代码即可 web2 题目:js把鼠标右键和f12屏蔽了 方法一: 禁用JavaScript 方法二: url前面加上view-source: web3 题 ...
- day16 Linux三剑客之awk
day16 Linux三剑客之awk 1.什么是awk,主要作用是什么? 什么是awk,主要作用是什么? awk 主要用来处理文件,将文本按照指定的格式输出.其中包含变量,循环以及数组. 2.awk的 ...
- Flink(三)【核心编程】
目录 一.Environment 二.Source 从集合读取数据 从文件读取数据 从kakfa读取数据(常用) 自定义数据源 三.Transform map Rich版本函数 flatMap key ...
- Linux学习 - 修改、查询文件内容
一.显示文件内容 cat [-n] [文件名] 正向显示 -n 显示行号 tac [文件名] 反向显示 more [文件名] 可实现分页显示 (空格)或(f) 翻页 (Enter) 换行 (q ...
- SpringBoot中使用JUnit4(入门篇)
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...