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:

  1. The number of nodes in the given tree will be in the range [1, 100].
  2. 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的更多相关文章

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

  2. 【Leetcode_easy】965. Univalued Binary Tree

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

  3. 965. Univalued Binary Tree

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

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

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

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

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

  7. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  8. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  9. 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

随机推荐

  1. SQL中的ALL,ANY,SOME的用法

    准备两个表: --T1(2,3)--T2(1,2,3,4) --ALL,ANY,SOME 的子查询-- >ALL 父查询中的结果集大于子查询中每一个结果集中的值,则为真SELECT * FROM ...

  2. C#从http上拿返回JSON数据

    C#如何拿到从http上返回JSON数据? 第一章:C#如何拿到从http上返回JSON数据? 第二章:C#如何解析JSON数据?(反序列化对象) 第三章:C#如何生成JSON字符串?(序列化对象) ...

  3. kali系统固化到固态硬盘小记(赠送给广大折腾党的笔记)

    1.首先你需要一个移动硬盘和一个移动硬盘盒子(一根数据转换线,一般买盒子商家会赠送的) SSD硬盘要事先格式化一下格式,不然识别不出来 2.准备好Kali镜像,传送门在这里https://www.ka ...

  4. GitHub-分支管理03-多人合作【重点】

    参考博文:廖雪峰Git教程 1. 多人协作 当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin. 要查看远程库的信 ...

  5. Mac轻量级服务器http-server

    刚想跑个Vue页面,发现我本地没有应用服务器(Tomcat/IIS...) 于是想下载了Tomcat,才发现我没有装JDK,而Mac的JDK下得好久,都下不下来,想想算了. 于是在网上找个轻量级的服务 ...

  6. 英语初级学习系列-00-Hello-打招呼

    开场白 当二咪过生日的时候,作为主人公(host)该如何开场呢? 1. Good evening, ladies and gentlemen. 先生们女士们,晚上好. 2. Welcome to my ...

  7. [WeChall] Training: Crypto - Caesar I (Crypto, Training)

    Training: Crypto - Caesar I (Crypto, Training) Crypto - Caesar I As on most challenge sites, there a ...

  8. 06.Python网络爬虫之requests模块(2)

    今日内容 session处理cookie proxies参数设置请求代理ip 基于线程池的数据爬取 知识点回顾 xpath的解析流程 bs4的解析流程 常用xpath表达式 常用bs4解析方法 引入 ...

  9. centos7下安装docker(16.1docker跨主机存储--Rex-Ray)

    一.Rex-Ray以standalone进程的方式运行在docker主机上,安装方法很简单:在docker1和docker2上运行如下命令: curl -sSL https://dl.bintray. ...

  10. 转://Oracle 高可用技术与云基础架构

    众所周知Oracle云基础架构已经在越来越多的行业里应用.大家了解云基础架构是如何演进的嘛?可能有人会说Oracle高可用技术是组成云架构的基础,那它们的关系是怎么样的?大家又了解Oracle高可用技 ...