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

只有给定的树是单值二叉树时,才返回 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. WPF 新突破

    刘琦告诉我  需要改变哪个属性哪个属性就需要实现InotifyPropertyChanged 董秀伟告诉我  界面改变会立即传输到属性上,并不需要实现InotifyPropertyChanged,只有 ...

  2. Vim统计字符串出现次数

    关键命令: :%s/pattern//gn 参数说明: % - 指明操作区间,%表示全文本:可以使用1,$或者行区间代替 s – substitute,表示替换 pattern - 要查找的字符串 / ...

  3. [JZOJ3303] 【集训队互测2013】城市规划

    题目 题目大意 求\(N\)个点的简单无向图的方案数(有编号). 结果对\(1004535809\)取模. 思考历程 感觉这个问题非常经典. 当时想到了一堆式子,但都觉得可能会有重和漏,于是弃掉了-- ...

  4. jquery高级编程学习

    jquery高级编程 第1章.jQuery入门 类型检查 对象 类型检查表达式 String typeof object === "string" Number typeof ob ...

  5. NPM一Node包管理和分发工具

    NPM 全称 Node Package Manager Node包管理和分发工具,可以把NPM理解为前端的Maven 我们通过npm可以很方便地下载js库,管理前端工程 最近版本的node.js已经集 ...

  6. thinkphp present标签

    present标签用于判断某个变量是否已经定义,用法: 大理石平台精度等级 <present name="name"> name已经赋值 </present> ...

  7. C++——虚析构

    目的: //只执行了 父类的析构函数//向通过父类指针 把 所有的子类对象的析构函数 都执行一遍//向通过父类指针 释放所有的子类资源 方法:在父类的析构函数前+virtual关键字 #define ...

  8. el-upload文件上传组件

    一.介绍 element-ui的组件之一,用来点击上传文件 官方是使用 before-upload 限制用户上传的图片格式和大小.但是某些浏览器不支持此方法,所以使用on-change来代替. 二.代 ...

  9. dart中的typedef <函数别名>

    typedef定义如下: typedef 给某一种特定的函数类型起了一个名字,可以认为是一个类型的别名.或者这样理解: 自己定义了一种数据类型,不过这种数据类型是函数类型,按照这种类型实例化后的对象, ...

  10. dart中extends、 implements、with的用法与区别

    一.概述 继承(关键字 extends) 混入  mixins (关键字 with) 接口实现(关键字 implements) 这三种关系可以同时存在,但是有前后顺序: extends -> m ...