题目地址:Univalued Binary Tree - LeetCode



Acceptance: 67.6%
Difficulty: Easy


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


这题的意思是判断一个二叉树的所有节点的值是否相同。


python3代码:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def isUnivalTree(self, root: TreeNode) -> bool:if root==None:return Trueif (root.left!=None and root.left.val!=root.val) or (root.right!=None and root.right.val!=root.val):return Falsereturn self.isUnivalTree(root.left) and self.isUnivalTree(root.right)

Java代码:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {public boolean isUnivalTree(TreeNode root) {if (root==null){return true;}if( (root.left!=null && root.left.val!=root.val) || (root.right!=null && root.right.val!=root.val)){return false;}return isUnivalTree(root.left) && isUnivalTree(root.right);}
}

java运行就是比Python快,只要2ms。


官方参考解法:

class Solution(object):def isUnivalTree(self, root):left_correct = (not root.left or root.val == root.left.valand self.isUnivalTree(root.left))right_correct = (not root.right or root.val == root.right.valand self.isUnivalTree(root.right))return left_correct and right_correct

LeetCode 965 Univalued Binary Tree--判断二叉树的所有节点的值是否相同--python,java解法相关推荐

  1. 965. Univalued Binary Tree

    965. Univalued Binary Tree 如果二叉树每个节点都具有相同的值,那么该二叉树就是单值二叉树. 只有给定的树是单值二叉树时,才返回true,否则返回false. # Defini ...

  2. [LeetCode][JavaScript]Invert Binary Tree 反转二叉树

    反转二叉树 其实我从没有想到前端面试会问到这个问题,题目来源于google的面试 Google: 90% of our engineers use the software you wrote (Ho ...

  3. LeetCode 654. Maximum Binary Tree最大二叉树 (C++)

    题目: Given an integer array with no duplicates. A maximum tree building on this array is defined as f ...

  4. [LeetCode] Binary Tree Paths - 二叉树基础系列题目

    目录: 1.Binary Tree Paths - 求二叉树路径 2.Same Tree - 判断二叉树相等 3.Symmetric Tree - 判断二叉树对称镜像 Binary Tree Path ...

  5. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++...

    LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++ Given preo ...

  6. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  7. LeetCode 110 Balanced Binary Tree

    LeetCode 110 Balanced Binary Tree Problem Description: 判断二叉树是不是平衡二叉树.所谓平衡二叉树,即每个节点的两个子树深度差的绝对值不超过1. ...

  8. LeetCode 226. Invert Binary Tree--反转二叉树--C++,Python解法--递归,迭代做法

    题目地址:Invert Binary Tree - LeetCode Invert a binary tree. Example: Input: 4/ \2 7/ \ / \ 1 3 6 9 Outp ...

  9. leetcode 971. Flip Binary Tree To Match Preorder Traversal

    leetcode 971. Flip Binary Tree To Match Preorder Traversal 题意:给一颗二叉树,再给一个的数组,能否通过交换两个左右两个子节点,使得二叉树的前 ...

最新文章

  1. rownum 的一点儿研究
  2. Windows10系统变慢,程序员该出手了,电脑性能大幅度提升!
  3. iOS自动化探索(九)使用Jenkins自动化打包并发布iOS App
  4. quick sort
  5. createprocess失败代码2_极客战记[森林]:边地之叉-通关代码及讲解
  6. vs c语言程序调试方法,VS2015中的常用调试技巧分享
  7. paste shell 分隔符_shell基础11:文件分类、合并和分割(sort,uniq,join,cut,paste,split)
  8. linux环境下内存溢出分析MAT
  9. 如何给namedtuple添加docstring
  10. 不要在变量名的旁边加echo和.br;
  11. 关于大小端、位域的一些概念详解
  12. 虚拟机中qemu模拟开发板启动过程,使用nfs挂载根文件系统
  13. Linux 安装mongodb数据库
  14. C语言LMS双麦克风消噪算法,一种双麦克风语音降噪方法与流程
  15. 计算机安全威胁防范,计算机网络安全威胁及防范措施
  16. 飞机大战的常见Bug
  17. 微信开发者工具报错Cannotreadpro ‘getPreloadAdUnitIds‘ of undefinedat Object.dK [as getPreloadAdUnitIds]
  18. vue页面导出Word文档(含图片)
  19. 【opencv-python】视频处理(5) cv2.VideoCapture.grab()函数、cv2.VideoCapture.retrieve()函数
  20. 防刷新网站访问量计数器

热门文章

  1. 深度学习在小分子药物研发中的应用
  2. 【3月30日直播】新冠病毒全基因组测序——Midnight试剂盒及整体解决方案
  3. 包邮免费送2022年科研人专属趣味台历,微科盟年终福利,错过悔一年!
  4. npj Microbiomes|细菌群落的整体涌现特性诱导了拟南芥的干旱抗性(一作解读)
  5. 134人!国家重点研发计划“生物大分子与微生物组”重点专项会评专家名单
  6. 2019微生物组—宏基因组分析专题研讨会
  7. cookie追加数据_集算器 SPL 抓取网页数据
  8. pandas对dataframe的数据行进行随机抽样(Random Sample of Rows):使用sample函数进行数据行随机抽样(有放回的随机抽样,replacement)
  9. R语言使用caret包构建GBM模型:在模型最优参数已知的情况下,拟合整个训练集,而无需进行任何重采样或参数调优
  10. 机器学习特征筛选:相关系数法(correlation)