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 ...
随机推荐
- java----鲁棒性
所谓“鲁棒性”,是指控制系统在一定(结构,大小)的参数摄动下,维持其它某些性能的特性. Java鲁棒性的特点如下: Java在编译和运行程序时都要对可能出现的问题进行检查,以防止错误的产生. Java ...
- raid1 raid2 raid5 raid6 raid10的优缺点和做各自raid需要几块硬盘
Raid 0:一块硬盘或者以上就可做raid0优势:数据读取写入最快,最大优势提高硬盘容量,比如3快80G的硬盘做raid0 可用总容量为240G.速度是一样.缺点:无冗余能力,一块硬盘损坏,数据全无 ...
- [UGUI]游戏中的Tips贴图标边缘显示(贴边)
Tips贴图标边缘显示 图左:当左边宽度不足于容纳Tips时,Tips放在右侧显示,顶和图标对齐 图右:当左边宽度足够容纳Tips时,Tips放在左侧显示,顶和图标对齐 适应需求:当图标和Tips是在 ...
- python爬虫之天气预报网站--查看最近(15天)的天气信息(正则表达式)
python爬虫之天气预报网站--查看最近(15天)的天气信息(正则表达式) 思路: 1.首先找到一个自己想要查看天气预报的网站,选择自己想查看的地方,查看天气(例:http://www.tianqi ...
- 解决 win10 由于磁盘缓慢造成机器迟钝
关闭 windows 的superfetch服务 建议禁止 superfetch服务: http://www.360quan.com/safe/6946.html 操作: http://jingyan ...
- MATLAB—求直线或者线段之间的交点坐标
function CrossPoint( ) %% 求两条直线的交点坐标 x1 = [7.8 8]; y1 = [0.96 0.94]; %line2 x2 = [8.25 8.25]; y2 = [ ...
- 更高的压缩比,更好的性能–使用ORC文件格式优化Hive
http://lxw1234.com/archives/2016/04/630.htm 关键字:orc.index.hive Hive从0.11版本开始提供了ORC的文件格式,ORC文件不仅仅是一种列 ...
- 自适应:用JS做的自适应,是最差的自适应,记页面刷新前后尺寸变化
今天遇到一个硬茬,我在使用weui重构一个页面时,出现一个问题:路由进入页面时,页面内容尺寸硬是会变大,刷新后又恢复正常: 项目背景:一个使用react-starter-kit构建的B端SPA项目 上 ...
- 修改CentOS默认yum源为国内yum镜像源
CentOS默认的yum源不是国内的yum源,在通过yum安装一些软件的时候,会出现这样那样的错误,以及在下载安装的速度上也是非常慢的. 所以这个时候就需要将yum源替换成国内的yum源,国内主要开源 ...
- 什么是数据库ACID?
原子性:由于操作失败导致的数据碎片错误: 一致性:由于并发导致的数据库数据错误(与预期不一致): 隔离性:由于并发导致的当前使用数据(应用端)错误: 事务在当今的企业系统无处不在,即使在高并发环境下也 ...