Count Univalue Subtrees

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.

For example:
Given binary tree,

              5
/ \
1 5
/ \ \
5 5 5

return 4.

分析:

  有点像自低向上的动态规划,既然是自底向上,看来用递归访问树的节点无疑可以解决问题

代码:

bool dfs(TreeNode *node, int &count) {
if(!node)
return true;
//一定要让保证其先递归,达到最底层节点,然后作后续处理
bool goodleft = dfs(node->left, count), goodright = dfs(node->right, count);
//与左右节点值相同,且左右子树是值相同的树,则以该节点为根节点的树也是值相同的树
if(goodleft && goodright && (!node->left || node->val == node->left->val) && (!node->right || node->val == node->right->val)) {
count++;
return true;
}
return false;
}
int count(TreeNode *node) {
int num = ;
dfs(node, num);
return num;
}

[Locked] Count Univalue Subtrees的更多相关文章

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

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

  2. 250. Count Univalue Subtrees

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

  3. [LeetCode#250] Count Univalue Subtrees

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

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

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

  5. [leetcode]250. Count Univalue Subtrees统计节点值相同的子树

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

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

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

  7. [LC] 250. Count Univalue Subtrees

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

  8. LeetCode-Count Univalue Subtrees

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

  9. [LeetCode] Longest Univalue Path 最长相同值路径

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

随机推荐

  1. ASP.NET和支付宝合作开发第三方接口的注意事项

    最近公司和支付宝合作开发第三方接口的项目,这里把过程中需要注意的地方说明一下: 前提:一般来说单个银行不接收个人或私企开通支付接口.因此,和第三方支付公司合作,签订合约开放接口就是通行的做法. 流程: ...

  2. VS2015 Cordova Ionic移动开发(二)

    一.创建空白Cordova应用 打开VS,选择[新建项目],选择其它语言JavaScript或者TypeScript,语言的话就按个人喜好,喜欢JS就用JS,喜欢TS就用TS,推荐使用TS书写,代码结 ...

  3. 关于UNION和UNION ALL的区别

    今天在运行程序的时候发现个问题,就是计算和的时候两条数据一样的话自动去除重复的,可是我这个程序需要重复的数据也算进来呀,然后就找原因,最后在sql语句中找到了是union和union all的问题,简 ...

  4. 制作SSL证书

    上一节介绍了OpenSSL的目录结构,本节介绍一下SSL证书的制作. OpenSSL安装后建议把其中的bin目录添加到系统环境变量中,方便以后操作. 建立一个新的目录SSL专门用来制作证书. 建立证书 ...

  5. iOS6以后的单个控制器横竖屏显示以及旋转屏控制技巧,附带iOS8以后显示电池状态栏

    一.在应用中从竖屏模式强制转换为横屏模式 第一种方法:通过模态弹出视图的方式,使得特定ViewController坚持特定的interfaceOrientation(1)iOS6之后提供了这样一个方法 ...

  6. cocos2d中如何使用图片纹理图集的加载来实现一个动画的功能

    cocos2d中要实现一个动画,一般采用纹理图集的方式,也就是说把几个连续动作的图片挨个显示切换这样就是动画 一: 首先先看下今天要实现的具体的目的,打飞机的时间屏幕上会有一个喷火的小飞机,飞机的尾部 ...

  7. VisualStudio2013内置SQLServer入门

    最近做项目老大要求用到sqlserver,但是这项目的数据库只是本地演示用并不复杂,于是决定试试VisualStudio2013内置的SQLServer.对于这个东西的了解并没有多少,然后项目初学习的 ...

  8. vlc_input buffer管理 & 时钟同步(转)

    vlc_input buffer管理 & 时钟同步 一.背景1.当播放网络视频流时(比如udp视频流),发送方(编码)和接收方(解码)是并行操作的,如果发送太慢(或因为网络原因出现延迟)的话, ...

  9. java.lang.String类compareTo()返回值解析

    一.compareTo()的返回值是int,它是先比较对应字符的大小(ASCII码顺序)1.如果字符串相等返回值02.如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值(ascii码值 ...

  10. 浏览器JS报错Uncaught RangeError: Maximum call stack size exceeded?

    JavaScript错误:Uncaught RangeError: Maximum call stack size exceeded 堆栈溢出 原因:有小类到大类的递归查询导致溢出 解决方法思想: A ...