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 ...
随机推荐
- vue组件参数校验
代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- css---盒模型新增样式
box-shadow 以逗号分割列表来描述一个或多个阴影效果,可以用到几乎任何元素上. 如果元素同时设置了 border-radius ,阴影也会有圆角效果.多个阴影时和多个 text shadows ...
- mysql 存储过程学习 汇总
存储过程框架 DEMILITER $$ -- 重定义符 DROP PROCEDURE IF EXISTS store_procedure$$ -- 如果存在此名的存储过程,先删除 CREATE PRO ...
- phonegap 开发指南系列----开始之前(1)
在基于任何平台(安卓.ios等phonegap支持的平台)上做phonegap开发之前,需要安装 cordova 的 command-line interface (CLI) .CLI详细:http: ...
- mui--使用mui中的图文表格组件时出现一条横线的解决方法
最近做的微信公众号点击链接跳转到H5页面,该H5页面使用mui做的,遇到的商品列表页出现一个横线的问题, 这个是修改前的图片 解决方法: <style type="text/css&q ...
- 安装rancher以及使用rancher倒入kubernetes集群和添加及管理集群
1.docker安装rancher [root@rancher ~]# docker run -d --name rancher --restart=unless-stopped -p : -p : ...
- nginx的配置:目的是使用nginx反向代理后,应用程序获取用户真实ip
一.了解nginx Nginx是lgor Sysoev为俄罗斯访问量第二的rambler.ru站点设计开发的.从2004年发布至今,凭借开源的力量,已经接近成熟与完善. Nginx功能丰富,可作为HT ...
- (转)谈谈Android中的Rect类——奇葩的思维
最近在工作中遇到了一些问题,总结下来就是Android中Rect这个类造成的.不得不说,不知道Android SDK的开发人员是怎么想的, 这个类设计的太奇葩了.首先介绍一下Rect类:Rect类主要 ...
- Unity3D Input 键盘控制
function Update (){ //Input.GetKey ("down") == Input.GetKey(KeyCode.DownArrow) if (Input.G ...
- Linux和Windows下ping命令详解
转:http://linux.chinaitlab.com/command/829332.html 一.Linux下的ping参数 用途 发送一个回送信号请求给网络主机. 语法 ping [ -d] ...