Given a binary tree, count the number of uni-value subtrees.

A Uni-value subtree means all nodes of the subtree have the same value.

Example :

Input:  root = [5,1,5,5,5,null,5]

              5
/ \
1 5
/ \ \
5 5 5 Output: 4

题意:

给定一棵二叉树,求所有节点都同值的二叉树。

思路:

dfs

代码:

 class Solution {
int result;
public int countUnivalSubtrees(TreeNode root) {
result = 0;
helper(root);
return result;
} private boolean helper(TreeNode node){
if(node == null ) return true; boolean left = helper(node.left);
boolean right = helper(node.right); if(left&&right){
if ((node.left!= null && node.val != node.left.val) || (node.right!= null && node.val != node.right.val) ){
return false;
}
result++;
return true;
}
return false;
}
}

[leetcode]250. Count Univalue Subtrees统计节点值相同的子树的更多相关文章

  1. [LeetCode] 250. Count Univalue Subtrees 计算唯一值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  2. [LeetCode#250] Count Univalue Subtrees

    Problem: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all ...

  3. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  4. 250. Count Univalue Subtrees

    题目: Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes ...

  5. [LC] 250. Count Univalue Subtrees

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  6. [Locked] Count Univalue Subtrees

    Count Univalue Subtrees Given a binary tree, count the number of uni-value subtrees. A Uni-value sub ...

  7. [Swift]LeetCode250.计数相同值子树的个数 $ Count Univalue Subtrees

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

  8. [LeetCode] 687. Longest Univalue Path 最长唯一值路径

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  9. [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

随机推荐

  1. nginx的日志分析

    1.到NGINX把日志DOWN下来2.用命令cat xxxx.log | egrep '10/Jul/2015:01:[4-5]|2015-07-10 02:0[0-57]'>xxxx2.log ...

  2. tp5模型笔记---多对多

    关联模型 一对一:HAS_ONE  以及对应的BELONEGS_TO 一对多:HAS_MANY 以及相对的BELONGS_TO 多对多:BELONGS_TO_MANY 步骤: 第一:创建Users模型 ...

  3. VMware仅主机模式访问外网

    原文转载至:https://blog.csdn.net/eussi/article/details/79054622 保证VMware Network Adapter VMnet1是启用状态  将可以 ...

  4. PLSQL导出表结构

    1:进行plsql后选怎Tools--------->Exports User Ojbects------------->选中需要导出的table,squence,view,type,fu ...

  5. Protocol Buffer Basics: Python

    原文https://developers.google.com/protocol-buffers/docs/pythontutorial Protocol Buffer Basics: Python ...

  6. Linux火焰图-centos

    centos7.5mini安装 yum install -y yum-utils perf debuginfo-install -y perf #debuginfo-install下载了305MB的文 ...

  7. margin-top失效

    span标签是行类元素,只能margin-left,right 解决办法: 将span标签改为块级标签

  8. TCP/IP协议和IP

    理解 使用网络能够把多方链接在一起,然后可以进行数据传递 所谓的网络编程就是,让在不同的电脑上的软件能够进行数据传递,即进程之间的通信 tcp/ip简介 1. 什么是协议 有的说英语,有的说中文,有的 ...

  9. 65. sqlserver执行存储过程实例

    declare @param varchar(500)exec sp_PUB_GetFlowStatus @ret output,10011,88,1,12print @ret

  10. archlinux上安装sublime text

    因为sublime text不是开源所以在官方库没有收纳,但是在archlinuxcn上面有. #vim /etc/pacman.conf 在末尾添加: [archlinuxcn] SigLevel ...