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. 浅谈TypeScript

    TypeScript为JavaScript的超集(ECMAScript6), 这个语言添加了基于类的面向对象编程.TypeScript作为JavaScript很大的一个语法糖,本质上是类似于css的l ...

  2. SharePoint Online 创建门户网站系列之创建栏目

    前 言 SharePoint Online的栏目,简单描述即显示在首页上的各个模块信息,这里,我们主要介绍我们首页上的栏目,包括简介类型.新闻列表类型.图片类型: 下面,让我们开始在SharePoin ...

  3. 奇葩问题:This file could not be checked in because the original version of the file on the server was moved or deleted. A new version of this file has been saved to the server, but your check-in comments were not saved

    "This file could not be checked in because the original version of the file on the server was m ...

  4. sqlite API模型

     每一个数据库连接可以包括多个数据库文件,一个主数据库文件和attached的几个数据库文件. 每一个数据库文件都有自己的B-tree和pager. 数据库连接(connection)和事务(tra ...

  5. Android 隐式意图的配置

    本文地址:http://www.cnblogs.com/wuyudong/p/5677473.html,转载请注明源地址. <Android 显示意图激活另外一个Actitity>一文介绍 ...

  6. objective-c系列-NSMutableString

    ********************************************** NSMutableString为NSString的子类,除了父类的方法,NSMutableStirng还有 ...

  7. 【代码笔记】iOS-评分,支持我们

    一,效果图. 二,工程图. 三,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additi ...

  8. 【代码笔记】iOS-看图听故事

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFo ...

  9. OC中UITabelView

    1.概述 继承自UIScrollView,只能显示一列数据,只能纵向滑动.堪称UIKit里面最复杂的一个控件了,使用起来不算难,但是要用好并不容易.当使用的时候我们必须要考虑到后台数据的设计,tabl ...

  10. MonoDevelop编辑器中文乱码解决

    说解决乱码分几步,总共分三部! 1. Tools -> Options 2. 3.点击Font->点击TextEditor会出现下边选框,选取喜欢风格并且不乱码即可.