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 ...
随机推荐
- C# yield
C#中的yield可以应用在一个可迭代的方法中,我们必须真正理解此关键词,才能将它正确的应用到实际生产中.为了说明yield会出现让我们迷惑的结果,下面先定义一个MyObject类: class My ...
- JavaScriptOO.com – 快速找到你需要的 JS 框架
JavaScriptOO.com 集合了目前 Web 开发中最常用的422(截至目前)款 JavaScript 框架,你可以根据功能类别(Ajax,动画,图表,游戏等)进行过滤和排序,快速找到你需要的 ...
- win7系统下,vs2010一调式,vs就关闭要重启
进入我的文档 %appdata%\Microsoft\VisualStudio, 将 10.0 重命名.网上找的方法有些问题,可能找这路径很难找到啊. 于是自己 找了找 一般都在当前用户文件夹下 Ap ...
- 轻松掌握:JavaScript装饰者模式
装饰者模式 在传统的面向对象语言中,给对象添加功能常常使用继承的方式,但继承的方式会带来问题:当父类改变时,他的所有子类都将随之改变. 当JavaScript脚本运行时,在一个对象中(或他的原型上)增 ...
- 从零开始,做一个NodeJS博客(二):实现首页-加载文章列表和详情
标签: NodeJS 0 这个伪系列的第二篇,不过和之前的几篇是同一天写的.三分钟热度貌似还没过. 1 静态资源代理 上一篇,我们是通过判断请求的路径来直接返回结果的.简单粗暴,缺点明显:如果url后 ...
- JS中取整以及随机颜色问题
前言:感觉自己已经好久好久没有写博客了,最近都是在写在线笔记比较多.现在来到新公司了,昨天刚刚完成一个项目所以今天有空研究研究一下前端方面的技术.下午在看一个游戏代码的时候,发现了几个别人留下的不错的 ...
- MyEclispe 2015 CI 15发布(附下载)
MyEclipse 2015 CI 15带来了一些程序上的改进,包括可外部部署的JavaScript调 试,改进了 REST Inspect 和 WebSphere 框架支持,新增服务器连接器,另外还 ...
- FIM2010同步用户
在需要进行同步的来源MA进行同步 在需要进行导入的来源进行导入
- SharePoint2007:解决第二回收站大数据无法删除问题
Emptying the Second Stage Recycle Bin in SharePoint 2007 Look in your second stage recycle bin in ...
- 【转】Android NFC学习笔记
一:NFC的tag分发系统 如果想让android设备感应到NFC标签,你要保证两点 1:屏幕没有锁住 2:NFC功能已经在设置中打开 当系统检测到一个NFC标签的时候,他会自动去寻找最合适的acti ...