题目如下:

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 = None class 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)

【leetcode】965. Univalued Binary Tree的更多相关文章

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

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

  2. 【Leetcode_easy】965. Univalued Binary Tree

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

  3. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  4. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  5. 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)

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

  6. 【LeetCode】998. Maximum Binary Tree II 解题报告(C++)

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

  7. 【LeetCode】971. Flip Binary Tree To Match Preorder Traversal 解题报告(C++)

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

  8. 【LeetCode】114. Flatten Binary Tree to Linked List 解题报告(Python & C++ & Java)

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

  9. 【LeetCode】919. Complete Binary Tree Inserter 解题报告(Python & C++)

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

随机推荐

  1. git 初始化提交项目

    Git初始化本地已有项目,并推送到远端Git仓库操作1. 创建本地项目,在项目根目录执行git init命令git init 2. 在git服务器上创建一个仓库,这里使用GitHub创建一个仓库.例如 ...

  2. github gist 查看html

    gist GitHub Gist 指南 https://blog.csdn.net/yz18931904/article/details/80482166 通过修改hosts解决gist.github ...

  3. Jquery对象转js对象

    $(this)  Jquery对象 var sex=$(this).get(0); js对象 sex.style.display='block';

  4. cocos2D-X call JNIHelper

    #ifndef _WIN32 JNIEnv *j = JniHelper::getEnv(); if (j == nullptr || j == NULL) {test += "JNIEnv ...

  5. openwrt配置内核,加载air720 4G模块的USB串口设备

    1,进入openwrt源码包,键入 make menuconfig 2,配置如下 kernel modules  ---> USB Support---> <*> kmod-u ...

  6. security 页面测试

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  7. SpringMVC·form表单Date类型问题导致的400问题

    问题描述 前端传yyyy-MM-dd hh:mm:ss格式的时间其实是String类型导致JavaBean中的Date类型Setter报错,从而导致api请求400. 问题解决 我的解决方式: 在对应 ...

  8. 建站手册-职业规划:职业履历(CV)

    ylbtech-建站手册-职业规划:职业履历(CV) 1.返回顶部 1. http://www.w3school.com.cn/careers/career_cv.asp 2. 2.返回顶部 1. 履 ...

  9. PHP-模拟请求和操作响应

    模拟请求 fsockopen <?php // 建立连接 $link = fsockopen('localhost', '80'); define('CRLF', "\r\n" ...

  10. 90、Tensorflow实现分布式学习,多台电脑,多个GPU 异步试学习

    ''' Created on 2017年5月28日 @author: weizhen ''' import time import tensorflow as tf from tensorflow.e ...