Binary Tree: Write a function to return count of nodes in binary tree which has only one child.
June 8, 2015
我最喜欢的一道算法题目, 二行代码.
编程序需要很强的逻辑思维, 严密,我还没有很好训练自己.想一想, 二行代码, 五分钟就可以搞定; 最近这几天网上大家热议的 Homebrew 的作者 Max Howell 面试
Google 挂掉的一题, 二叉树反转, 七行代码, 相比二行代码, 情有可原!
One solution (Cannot pass the phone interview - need to improve, show reasoning, logic thinking, any rules can be applied?):
/**
* latest update; June 9, 2015
* https://juliachenonsoftware.wordpress.com/2015/06/09/binary-tree-write-a-function-to-return-count-of-nodes-in-binary-tree-which-has-only-one-child/
* Comment:
*/
public static int countOneChildNode1(Node node) {
if (node == null) return 0;if (node.left != null && node.right != null)
{
return countOneChildNode1(node.left) + countOneChildNode1(node.right);
}
else if (node.left != null)
{
return countOneChildNode1(node.left) + 1;
}
else if (node.right != null)
{
return countOneChildNode1(node.right) + 1;
}
else // no left child, no right child
return 0;
}
Solution B: two lines code:
/**
* https://juliachenonsoftware.wordpress.com/2015/06/09/binary-tree-write-a-function-to-return-count-of-nodes-in-binary-tree-which-has-only-one-child/
* Great arguments:
1. Since first line is the discussion of “node==null”, there is no need to check node!=null before the function countOneChildNode call; which is redundant,
waste of time, more code to maintain, and leave the check to the recursive function call, where the null checking is doing the job.
2. How to express only one child?
case 1: left child is not null; in other words, there is a left child: node.left!=null
case 2: right child is not null; node.right!=null)
case 3: node has two child
(node.left!=null) && (node.right!=null)
case 4: node has only one child (A: left child only, B: right child only, one true, one false; left child existed != right child existed; cannot be both false or both true)
(node.left!=null)!=(node.right!=null)
case 5: at least one child (one child or two child)
(node.left!=null) || (node.right!=null)
这道题非常好, 通过这道题, 你可以如何简化代码; 如果有一个出色的程序员, 有很强的逻辑思维能力, 想到只有一个孩子, 可以表示为一句话: (node.left!=null)!=(node.right!=null)
*/
public static int countOneChildNode(Node node)
{
if(node==null) return 0;
return (((node.left!=null)!=(node.right!=null)?1:0)+countOneChildNode(node.left)+countOneChildNode(node.right));
}
Github for source code:
Binary Tree: Write a function to return count of nodes in binary tree which has only one child.的更多相关文章
- js eval()函数 接收一个字符串,做为js代码来执行。 如: s='var d="kaka"'; 或者s=‘function (code){return code }’;
eval函数接收一个参数s,如果s不是字符串,则直接返回s.否则执行s语句.如果s语句执行结果是一个值,则返回此值,否则返回undefined. 需要特别注意的是对象声明语法“{}”并不能返回一个值, ...
- Invalid default value for prop "value": Props with type Object/Array must use a factory function to return the default value.(props default 数组/对象的默认值应当由一个工厂函数返回)
Invalid default value for prop "value": Props with type Object/Array must use a factory fu ...
- function $(id) { return typeof id === "string" ? document.getElementById(id) : id; }
function $(id) { return typeof id === "string" ? document.getElementById(id) : id; } 这句代 ...
- [].slice.call(k).filter(function(l) { return l != 0 });
[].slice.call(k).filter(function(l) { return l != 0 }); 将类数组调用数组方法.
- vue中报错Props with type Object/Array must use a factory function to return the default value
Invalid default value for prop "value": Props with type Object/Array must use a factory fu ...
- lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】
1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...
- Farthest Nodes in a Tree ---LightOj1094(树的直径)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...
- LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)
Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...
随机推荐
- 阿里巴巴开源Weex 开发教程
Weex 是什么 Weex是阿里发布的一款用WEB方式开发原生app的开源产品 Weex能够完美兼顾性能与动态性,让移动开发者通过简捷的前端语法写出Native级别的性能体验,并支持iOS.安卓.Yu ...
- var和dynamic的区别及如何正确使用dynamic ?
C#中的很多关键词用法比较容易混淆,var和dynamic就是其中一组,他们都可以申明动态类型的变量,但是本质上他们还是有不少区别的.var 在编译阶段已经确定类型,在初始化时候,必须提供初始化的值, ...
- CSS3中DIV水平垂直居中-2(3)
用到CSS3中display的新属性. HTML <div class="parent"> </div> CSS html,body{ width: 100 ...
- Wix安装程序中判断是否安装的.net framwork 4.5
<PropertyRef Id="NETFRAMEWORK40FULL"/> <PropertyRef Id="NETFRAMEWORK45" ...
- 超人学院二期学员分享hadoop工作经验
定于2月17日--2月23日的某一天,邀请咱们学员分享一下hadoop工作经验.对于没工作过的同学或者没从事过hadoop工作的同学,抓住机会啊,你可以提前准备自己关心的各种问题! 具体时间请关注QQ ...
- yii redies 不同的工程缓存key的问题
参考这篇文章 基本配置操作: yii main.php中: return array( ... 'components'=>array( 'redis_cache' => array ( ...
- UITableView全面解析
本文转自:http://www.cocoachina.com/ios/20140922/9710.html 在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以 ...
- sqlserver 附加数据库失败,错误提示:5拒绝访问 解决办法
sqlserver 附加数据库失败,错误提示:5拒绝访问 解决办法 金刚 sqlserver 附加数据库 拒绝访问 今天把项目拷贝到新硬盘里,发现在附加数据库中提示:操作系统错误5:"5拒绝 ...
- js 静态私有变量
特权方法:有权访问私有变量和私有函数的公有方法.在私有作用域中定义私有变量或函数,可以创建特权方法,如下: 示例1 (function(){ //私有变量和私有函数 var privateVariab ...
- C#问题
1.结构体里面是否可以有属性? 可以有属性.实测代码以及截图. In C#, we can use the following statement to convert a string s to a ...