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.
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]
.
题目描述:大概意思就是问我们给定一棵树,判断这棵树上的所有节点的值是不是相同的,相同即为 true
,不相同为 false
。
题目分析:判断一棵树的所有节点的值是不是相同的,可以分为以下几个条件:
- 节点是否为空
- 左子节点和父节点是否相同
- 右子节点和父节点是否相同
- 左子节点和右子节点是否相同
根据这个思路我们可以解决这个问题。
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
"""
left_correct = not root.left or root.val == root.left.val and self.isUnivalTree(root.left)
right_correct = not root.right or root.val == root.right.val and self.isUnivalTree(root.right)
return left_correct and right_correct
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:
int temp;
bool flag = true;
bool isUnivalTree(TreeNode* root) {
if(!root){
return true;
}
temp = root->val;
travelTree(root);
return flag;
}
void travelTree(TreeNode* root){
if(root){
travelTree(root->left);
travelTree(root->right);
if(flag){
flag = root->val == temp ? true : false;
}
}
}
};
LeetCode 965. Univalued Binary Tree的更多相关文章
- 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_easy】965. Univalued Binary Tree
problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完
- 965. Univalued Binary Tree
题目来源: https://leetcode.com/problems/univalued-binary-tree/submissions/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: c ...
- 【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 on ...
- LC 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 OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
随机推荐
- matlab练习程序(点云表面法向量)
思路还是很容易想到的: 1.首先使用KD树寻找当前点邻域的N个点,这里取了10个,直接调用了vlfeat. 2.用最小二乘估计当前邻域点组成的平面,得到法向量. 3.根据当前邻域点平均值确定邻域质心, ...
- java--反射机制总结
一.什么是Java反射机制? JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的以及动态调用对象的方法的功能 ...
- Node.js学习记录(一)--安装设置篇
安装Node window window上安装node可选择以下两种方式: 方式一:直接进入官网下载安装 进入node.js官网点击windows,选择.msi后缀的,根据自己的电脑选择对应的64位或 ...
- 【PHPStorm使用手册】php interpreter is not configured
php interpreter is not configured 未配置php解析器 第一步: 引入下载好的php.exe 打开窗口 file -> settings -> Langua ...
- 自动化测试基础篇--Selenium鼠标键盘事件
摘自https://www.cnblogs.com/sanzangTst/p/7477382.html 前面几篇文章我们学习了怎么定位元素,同时通过实例也展示了怎么切换到iframe,怎么输入用户名和 ...
- App分享之微信微博等各个社交平台的分享授权规则和常见问题
一.新浪微博分享规则 新浪微博支持分享类型: 应用内分享也就是网页分享支持: 文字,文字+图片,要分享链接需要链接添加在text里分享 客户端分享支持:文字,图片,文字+图片,图片+文字+链接 参数说 ...
- spring cloud 实践坑点记录
用spring cloud 微服务框架有一段时间了有一些坑点在这里给大家记录一下希望大家用得着 1.当我们使用聚合性能监控的时候,我们采用 rabbitmq作为消息中间件来收集性能信息最后在使用Tur ...
- C3P0连接池温习1
一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...
- 设计模式のAdapterPattern(适配器模式)----结构模式
一.产生背景 这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能.举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器.您将内存卡插入读卡器,再将读卡器插入笔记本,这样就可以通过笔记本 ...
- fabric使用
1.入门博客https://fabric-chs.readthedocs.io/zh_CN/chs/tutorial.html 如果遇到这个问题说明你的fabric版本太高了 卸载到现在版本重新安装就 ...