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. 利用Selenium爬取淘宝商品信息

    一.  Selenium和PhantomJS介绍 Selenium是一个用于Web应用程序测试的工具,Selenium直接运行在浏览器中,就像真正的用户在操作一样.由于这个性质,Selenium也是一 ...

  2. C# 枚举转列表

    using System; using System.Collections.Generic; using System.ComponentModel; namespace Common.Utils ...

  3. Ajax的一个实例及代码

    这是用ajax做的一个小小的应用!当选择menu1的时候,会出来menu里面所有的内容.同理对于menu2.多的不说,代码如下: 首先是inner.html文件 <html><hea ...

  4. LeetCode算法题-Minimum Depth of Binary Tree(Java实现)

    这是悦乐书的第168次更新,第170篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第27题(顺位题号是111).给定二叉树,找到它的最小深度.最小深度是沿从根节点到最近的 ...

  5. 【Linux基础】VM使用

    VM三种联网方法和原理 (1)Bridged桥接 使用VMnet0虚拟交换机,此时虚拟机相当与网络上的一台独立计算机与主机一样,拥有一个独立的IP地址,所有机器均可互访,可以联网.使用桥接方式,A,A ...

  6. nginx: [emerg] unknown directive "stub_status" in /usr/local/openresty/nginx/conf/conf.d/ngx_metric.conf:19

    问题分析 Nginx没有添加modules/ngx_http_stub_status_module.o模块. 问题解决 没有安装的话,可以在tar包安装编译的时候添加如下参数: # ./configu ...

  7. rem自适应

    //REM自适应 _resize(); window.addEventListener('resize', _resize, false); function _resize() { var devi ...

  8. C#のsocket通信

    博主要做一个手机和电脑端(C#)通讯的程序,便览了网络上关乎socket的东西.但是接收文件的时候卡住了,怎么也接收不全.后来做了分片处理,如果分片,发送的时候就会有不同的socket(客户端开发不是 ...

  9. 基于diango简易BLOG图片预览

    注册界面 登陆界面 博客主界面 个人站点主页 文章详情页 个人博客后台 添加文章页 修改密码页

  10. centos7下安装docker(17.5docker监控的总结对比)

    到现在为止,我已经学习了docker自带的监控方案:ps/ls/top/stats,以及sysdig,weave scope,cadvisor,prometheus多种监控工具,现在做个总结和比较 部 ...