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.的更多相关文章

  1. js eval()函数 接收一个字符串,做为js代码来执行。 如: s='var d="kaka"'; 或者s=‘function (code){return code }’;

    eval函数接收一个参数s,如果s不是字符串,则直接返回s.否则执行s语句.如果s语句执行结果是一个值,则返回此值,否则返回undefined. 需要特别注意的是对象声明语法“{}”并不能返回一个值, ...

  2. 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 ...

  3. function $(id) { return typeof id === "string" ? document.getElementById(id) : id; }

    function $(id) {   return typeof id === "string" ? document.getElementById(id) : id; } 这句代 ...

  4. [].slice.call(k).filter(function(l) { return l != 0 });

    [].slice.call(k).filter(function(l) { return l != 0 }); 将类数组调用数组方法.

  5. 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 ...

  6. lightoj 1094 Farthest Nodes in a Tree 【树的直径 裸题】

    1094 - Farthest Nodes in a Tree PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: ...

  7. Farthest Nodes in a Tree ---LightOj1094(树的直径)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no ...

  8. LightOJ1094 - Farthest Nodes in a Tree(树的直径)

    http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ...

  9. LightOJ--1094-- Farthest Nodes in a Tree(树的直径裸题)

    Farthest Nodes in a Tree Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu S ...

随机推荐

  1. Bootstrap左侧下拉三级菜单

    在线实例 效果一 效果二 效果三 使用方法 <script src="/api/jq/BootstrapCaidan/js/metismenu.js"></scr ...

  2. Web 开发中应用 HTML5 技术的10个实例教程

    HTML5 作为下一代网站开发技术,无论你是一个 Web 开发人员或者想探索新的平台的游戏开发者,都值得去研究.借助尖端功能,技术和 API,HTML5 允许你创建响应性.创新性.互动性以及令人惊叹的 ...

  3. transform:rotate在手机上显示有锯齿的解决方案大全

    先来个兼容性说明,洗洗脑: div{transform:rotate(7deg);-ms-transform:rotate(7deg); /* IE 9 */-moz-transform:rotate ...

  4. 【小贴士】zepto find元素以及ios弹出键盘可能让你很头疼

    前言 在此,我不得不说移动端的兼容问题很多,并且很令人头疼,这不,这个星期又有两个让我逮着了,一个是使用zepto过程中出现的问题,一个是ios虚拟键盘的问题 我这里做一次记录,以免以后忘了,同时希望 ...

  5. 【初探HTML本相】道之真谛不过自然,html标签脱俗还真

    前言 须弥般若有无空,阴阳道化真虚同:洗尽前恩本非相,还吾面目下九重. 咳咳,其实老夫对佛教文化有点点研究啦,说以我们这里来了一点很有哲理的东西,因为我这里准备干一件很戳的事情,我准备来看看我们的ht ...

  6. [deviceone开发]-纳豆项目源码开源

    一.简介 已上线的纳豆项目开源.这个App做的非常好,细节处理的很流畅,使用了很多小的动画效果,极力推荐,很多功能可以从这个应用中获取代码和灵感. 二.效果图        三.相关下载 androi ...

  7. 对于Access数据库查询遇到空值的解决办法

    1.Access数据库在office环境下对于null是识别的,但是,在开发环境下,Access数据库对于where xxx is null是不识别的. 2.查询空值解决办法:select * fro ...

  8. windows server2008 安装问题、sqlserver安装设置默认账户问题

    1.Bios中的satadata设置开启 2账户和密码最好与本机相同

  9. Office 365 – SharePoint 2013 Online 与Office相关的应用

    1.在Office 365首页,点击在PC上安装Office,如下图: 2.会下载一个安装向导程序,如下图: 3.安装Office向导,如下图: 4.安装完毕以后,发现开始菜单多出Office 201 ...

  10. webservice简单引用

    //1.创建网站//2.新建项=>添加web服务//运行texttweb.asmx可以通过访问http://域名/webservice/texttweb.asmx来验证了//3.添加服务引用=& ...