Leetcode965. Univalued Binary Tree单值二叉树
如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。
只有给定的树是单值二叉树时,才返回 true;否则返回 false。
示例 1:
输入:[1,1,1,1,1,null,1] 输出:true
示例 2:
输入:[2,2,2,5,2] 输出:false
提示:
- 给定树的节点数范围是 [1, 100]。
- 每个节点的值都是整数,范围为 [0, 99] 。
class Solution {
public:
int val = -1;
bool isUnivalTree(TreeNode* root)
{
if(root == NULL)
return true;
if(val < 0)
{
val = root ->val;
return isUnivalTree(root ->left) && isUnivalTree(root ->right);
}
else if(val != root ->val)
{
return false;
}
return isUnivalTree(root ->left) && isUnivalTree(root ->right);
}
};
Leetcode965. Univalued Binary Tree单值二叉树的更多相关文章
- 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化
遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 965. Univalued Binary Tree
题目来源: https://leetcode.com/problems/univalued-binary-tree/submissions/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: c ...
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
[104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...
- 【Leetcode_easy】965. Univalued Binary Tree
problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完
- [Swift]LeetCode965. 单值二叉树 | 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 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】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- 使用Fiddler模拟弱网络环境测试
https://blog.csdn.net/swordgirl2011/article/details/51765237 https://www.cnblogs.com/longronglang/p/ ...
- python中与time模块和datetime模块相关操作
使用time模块计时程序运行时长 import time time_start = time.time() #程序主体部分 time_end = time.time() print('总耗时: ', ...
- 树莓派3B+ 人脸识别、摄像头安装和使用
最近在学校里折腾树莓派上的人脸识别,折腾了很久才能用 在此记录下使用的过程和遇到的困难 过程基于超有趣!手把手教你使用树莓派实现实时人脸检测完成的.其中前面opencv的安装是文章中的Raspbian ...
- Xadmin+layUi Net框架
1.引用xadmin layui 2.autofac+dapper+mvc 3.效果展示 4.github https://github.com/sulin888/LsProject 登录密码:Ad ...
- MyEclipse搭建Structs2开发环境
MyEclipse10搭建Strust2开发环境 - 孤傲苍狼 - 博客园https://www.cnblogs.com/xdp-gacl/p/3496242.html
- vuecli脚手架+vue+vuex实现vue驱动的demo。
哎呀呀呀,现在大家都要会Vue || React,否则感觉跟这个前端的世界脱节了一样. start: vue-cli这个构建工具大大降低了webpack的使用难度,支持热更新,有webpack-de ...
- VS2010-MFC(图形图像:CDC类及其屏幕绘图函数)
转自:http://www.jizhuomi.com/software/244.html 上一节讲了文本输出的知识,本节的主要内容是CDC类及其屏幕绘图函数. CDC类简介 CDC类是一个设备上下文类 ...
- .NETFramework-Web.Mvc:HttpXxxAttribute-目录
ylbtech-.NETFramework-Web.Mvc:HttpXxxAttribute-目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返 ...
- POJ 1127 /// 判断线段与线段是否相交
题目大意: 给定n条线段 接下来n行是端点信息 接下来询问 a b 是否相交 若a与c相交 b与c相交 ,那么a与b就是相交的 先判断任两条线段是否相交 再用folyd #include <cs ...
- shell脚本练习06
######################################################################### # File Name: -.sh # Author ...