如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树。

只有给定的树是单值二叉树时,才返回 true;否则返回 false。

示例 1:

输入:[1,1,1,1,1,null,1] 输出:true

示例 2:

输入:[2,2,2,5,2] 输出:false

提示:

  1. 给定树的节点数范围是 [1, 100]。
  2. 每个节点的值都是整数,范围为 [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单值二叉树的更多相关文章

  1. 遍历二叉树 traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化

    遍历二叉树   traversing binary tree 线索二叉树 threaded binary tree 线索链表 线索化 1. 二叉树3个基本单元组成:根节点.左子树.右子树 以L.D.R ...

  2. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  3. 965. Univalued Binary Tree

    题目来源: https://leetcode.com/problems/univalued-binary-tree/submissions/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: c ...

  4. 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】

    [104-Maximum Depth of Binary Tree(二叉树的最大深度)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary t ...

  5. 【Leetcode_easy】965. Univalued Binary Tree

    problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完

  6. [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 ...

  7. 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 ...

  8. 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...

  9. [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

随机推荐

  1. 一道Oracle子查询小练习

    一道Oracle子查询小练习   昨天晚上躺在床上看Oracle(最近在学习这个),室友说出个题目让我试试.题目如下: 有如下表结构,请选择出成绩为前三名的人的信息(如果成绩相同,则算并列),表名为t ...

  2. SpringBatch批处理框架:入门项目

    1.项目结构如下:

  3. div中内容可左右上下滑动

    在<table>外套一层<div>,并且声明overflow:scroll属性,如: <div style="width:1620px;height:680px ...

  4. 深入解读阿里云数据库POLARDB核心功能会话读一致性

    POLARDB架构 我们知道,POLARDB是一个由多个节点构成的数据库集群,一个主节点,多个读节点.对外默认提供两个地址,一个是集群地址,一个是主地址,推荐使用集群地址,因为它具备读写分离功能可以把 ...

  5. C++——类的继承(派生)

    类的继承就是子类可以拥有父类的成员变量和成员函数 //public 修饰的成员变量 方法 在类的内部 类的外部都能使用//protected: 修饰的成员变量方法,在类的内部使用 ,在继承的子类中可用 ...

  6. 普通的maven项目变成web项目

    command+: 或者 这个修改同样可以解决idea中不能新建servlet的问题. 这里最后的目录结构是这样的,如果在上面的设置中尝试修改目录,会导致无法创建servlet,比如我希望将根目录改成 ...

  7. 阿里云CentOs7上安装JDK

    一.查看服务器是否已经预装了JDK 在拿到新机器以后,要先看下机器上是否已经预装了JDK,命令: rpm -qa|grep jdk 如果有的话,卸载openjdk(无需输全称).命令: yum -y ...

  8. day25-静态、组合、继承

    #!/usr/bin/env python # -*- coding:utf-8 -*- # ----------------------------------------------------- ...

  9. 第二十篇:记下第一个mysql触发器

    项目背景:给一个服务限制访问次数,当用户访问这个服务的次数达到这个值的时候,关闭他的访问权限首先访问信息存在一张表中,记录用户的ip:visitor_ip,服务的id:service_id,访问次数: ...

  10. vue通过修改element-ui相关类的样式修改element-ui组件的样式

    可以在App.vue中的style中修改element-ui的样式. .el-menu{ width:160px !important; } 注意:一定要在属性值后面加上 !important 使自己 ...