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. 25个有用的和方便的 WordPress 速查手册

    如果你是 WordPress 开发人员,下载一些方便的 WordPress 备忘单可以在你需要的时候快速查找.下面这个列表,我们已经列出了25个有用的和方便的 WordPress 速查手册,赶紧收藏吧 ...

  2. 教你写能被舒服舒服又舒服地调用的iOS库

    目录 前言 脑洞开一开 分析 整容 结语 前言 2014年过的那么快,过年又那么块,2015年又是飞快地节奏,真尼玛感觉上帝是不是无聊使用了变速外挂开启了加速模式~到现在博主都无法接受已经上班的事实… ...

  3. SAP (ABAP) 常用的数学函数

    Function func Return value abs Absolute value of the argument arg (绝对值) sign Plus/minus sign of the ...

  4. sharepoint 数据库说明

    一.WSS_Content 后端内容数据库存储所有网站内容,包括网站的文档或文档库中的文件,列表数据和Web部件属性,以及用户名和权限. 为特定网站的所有数据的内容数据库. 二.SharePoint_ ...

  5. 高性能JS笔记3——DOM编程

    一.访问与修改DOM DOM和JS 相当于两个岛屿,访问操作的次数越多,要交的过路费越多,对性能产生很大影响. 减少访问DOM的次数,把运算尽量留在JS端操作. 二.innerHTML 对比 DOM ...

  6. SharePoint2007:解决第二回收站大数据无法删除问题

    Emptying the Second Stage Recycle Bin in SharePoint 2007   Look in your second stage recycle bin in ...

  7. android 显示 PDF 文件

    1.开源项目地址 : https://github.com/JoanZapata/android-pdfview 2.引用 compile 'com.joanzapata.pdfview:androi ...

  8. IOS开发——02_第一个类的创建、声明…

    在OC中,一般用2个文件来描述一个类: 1..h:类的声明文件,用于声明成员变量.方法.类的声明使用关键字@interface和@end. 注:.h中只是用做方法声明,并不进行实现.什么叫声明呢?简单 ...

  9. iPhone 信号格转变数字

    ?在拨号键输入*3001#12345#* ?按呼叫键,就会进入Field Test页面 ?你就会看到信号格变为了数字,然而只要按home键返回就又会变为信号格 ?如果想一直变为数字的话,在Field ...

  10. android post带数据请求方式,传递的数据格式包括json和map

    如下: public static String httpPost(String url, String json) { try { URL u = new URL(url); HttpURLConn ...