博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【leetcode】965. Univalued Binary Tree
阅读量:7052 次
发布时间:2019-06-28

本文共 1082 字,大约阅读时间需要 3 分钟。

题目如下:

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

解题思路:送分题。

代码如下:

# Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution(object):    def recursive(self,node,v):        if node.val != v:            return False        r1 = r2 = True        if node.left != None:            r1 = self.recursive(node.left,v)        if node.right != None:            r2 = self.recursive(node.right,v)        return r1 and r2    def isUnivalTree(self, root):        """        :type root: TreeNode        :rtype: bool        """        if root == None:            return True        return self.recursive(root,root.val)

 

转载于:https://www.cnblogs.com/seyjs/p/10201656.html

你可能感兴趣的文章
Atari 游戏得分提升两个数量级:Uber AI 的新强化学习算法 Go-Explore
查看>>
4月1日云栖精选夜读 | 代号“凤凰”,阿里新零售秘密武器,今年要打入100个城市...
查看>>
swap file在btrfs分区Invalid argument问题
查看>>
C++面向对象高级编程(上) 第二周 侯捷
查看>>
Python多版本情况下四种快速进入交互式命令行的操作技巧
查看>>
如何在基于Bytom开发过程中集成IPFS
查看>>
后台管理,给列表页新增查询功能,所遇到的问题及感想
查看>>
GraalVM 社区版 1.0 RC15 发布,新一代高性能跨语言虚拟机
查看>>
阿里架构师眼里JVM可以说的那些事
查看>>
C#实现局部峰值查找,功能对应Matlab中的findpeaks.m
查看>>
响应式编程
查看>>
The Road to learn React书籍学习笔记(第一章)
查看>>
WPF 自定义控件的坑(蠢的:自定义控件内容不显示)
查看>>
Confluence 6 空间标识
查看>>
使用kubeadm安装Kubernetes v1.10以及常见问题解答
查看>>
Linux FTP上传脚本
查看>>
Robot Framework之pymysql数据库查询
查看>>
干货 | 机器学习没有你想的那么复杂
查看>>
PostgreSQL 10.1 手册_部分 III. 服务器管理_第 16 章 从源代码安装_16.1. 简单版
查看>>
springMVC的事务不回滚
查看>>