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 ...
随机推荐
- 成为 Web 开发大师你必须知道的 7 件事情
曾经是这样的,懂点编码,并可以偶尔耍点酷,那么你就会被认为是一个Web开发大师.但是现在,情况再也不是这样的了.Web开发已经朝着主流方向发展,开发人员数量显著增加.这意味着,如果你想成为这个领域的大 ...
- 3种不同的ContextMenu右键菜单演示
简单使用的右键菜单,希望能帮助大家.下面是截图和实例代码 实例预览 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional/ ...
- Rotating Image Slider - 图片旋转切换特效
非常炫的图片旋转滑动特效,相信会给你留下深刻印象.滑动图像时,我们会稍稍旋转它们并延缓各元素的滑动.滑块的不寻常的形状是由一些预先放置的元素和使用边框创建.另外支持自动播放选项,鼠标滚轮的功能. 在线 ...
- 乱码之MyEclipse控制台
今天突然发现控制台出现乱码,查了资料解决方案不一. 我的解决方案如下: Run -> Debug Configuration... -> MyEclipse Servler -> M ...
- 【追寻javascript高手之路01】javascript参数知多少?
前言 我最近在思考一个问题,我本身平时还是积累了不少东西,面试时候问的东西基本逃不出写的博客(当然,高级阶段的就不行了),但是真的被问到时我却不一定答得上来. 知道且能回答,回答的效果都不是很好... ...
- 【position也可以很复杂】当弹出层遇上了鼠标定位(下)
前言 接着昨天的内容写,为了保证内容连续性,这里还是把昨天的内容拷了过来. 请用现代浏览器测试 引出问题 有图有真相,我们来看一个智联招聘里面经常出现的图层: 他这个是没有什么问题的,我们来简单看看其 ...
- [deviceone开发]-模仿Zaker的示例
一.简介 这个示例模仿zaker这个App,主要的界面基本都完成,用到了各种deviceone提供的ui组件,比如GridView,ListView,ScrollView,ViewShower等等.初 ...
- .net弹出框
弹出框可以使用div来显示在前台增加 <div id="flowDiv" runat="server"> <div class="r ...
- 【Leafletjs】7.结合echart图表展示信息
1.popup中添加图表信息 //定义marker var marker = L.marker(val.location).addTo(map); var content = '<div sty ...
- .Net关闭数据库连接时判断ConnectionState为Open还是Closed?
两种写法: if (conn.State == System.Data.ConnectionState.Open) { conn.Close(); ...