LeetCode 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 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:
- The number of nodes in the given tree will be in the range
[1, 100]. - 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的更多相关文章
- 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_easy】965. Univalued Binary Tree
problem 965. Univalued Binary Tree 参考 1. Leetcode_easy_965. Univalued Binary Tree; 完
- 965. Univalued Binary Tree
题目来源: https://leetcode.com/problems/univalued-binary-tree/submissions/ 自我感觉难度/真实难度: 题意: 分析: 自己的代码: c ...
- 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【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 ...
- 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 ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
随机推荐
- VS开发程序用户防范安全问题
一个开发的系统程序从需求.设计到打包.用户使用的过程中,安全问题一直是开发者关注的焦点.对于用户来说,不考虑加密工具(如加密精灵等),面对的是一个系统的各个组件集合及各类的配置文件( 如App.Con ...
- Centos7开启ssh免密码登录
1.输入命令:cd .ssh进入rsa公钥私钥目录(清空旧秘钥) 2.在当前目录下执行ssh-keygen -t rsa,三次回车后生成新的公钥(id_rsa.pub)私钥(id_rsa)文件(每个节 ...
- WSL优化 (Windows Subsystem for Linux) Linux子系统优化配置
目录 wsl优化 (Windows Subsystem for Linux) Linux子系统优化 1. 永久修改wsl终端字体 2. 修改Linux终端配色 3. 添加WSL到右键菜单 wsl优化 ...
- 【redis专题(7)】命令语法介绍之Pub/Sub
Redis 发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息.主要的目的是解耦消息发布者和消息订阅者之间的耦合,这点和设计模式中的观察者模式比较相似.p ...
- C# 函数1 (函数的定义)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 na ...
- SQL Server 一致性读
我们在Oracle和MySQL数据库中已经对一致性读的概念比较熟悉了,但是在SQL Server中却鲜少提及,但SQL Server自2005版本以来其实也实现了一致性读,几乎所有关系型数据库产品的一 ...
- linux c 语言之--fseek(),fseeko(),fseeko64()讲解 (转载)
转载:http://blog.csdn.net/lemoncyb/article/details/16841317 fseek() 函数讲解: 函数定义: int fseek(FILE *stream ...
- Kernel数据结构移植(list和rbtree)
主要移植了内核中的 list,rbtree.使得这2个数据结构在用户态程序中也能使用. 同时用 cpputest 对移植后的代码进行了测试.(测试代码其实也是使用这2个数据结构的方法) 内核代码的如下 ...
- FCM算法的matlab程序
FCM算法的matlab程序 在“FCM算法的matlab程序(初步)”这篇文章中已经用matlab程序对iris数据库进行简单的实现,下面的程序最终的目的是求准确度. 作者:凯鲁嘎吉 - 博客园 h ...
- Python开发【第二篇】:基本数据类型
运算符 1.算数运算 2.比较运算 3.赋值运算 4.逻辑运算 5.成员运算 基本数据类型 所有对象所具备的方法都保存在类中.对象和类的关系,举个例子:哺乳动物是类:此类下有两个对象,一个为狗.一个为 ...