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 ...
随机推荐
- 20个最新的照片 PS 技巧,提升摄影水平
相信很多人都知道 Photoshop 在照片编辑方面的强大,所以几乎每张照片经过 PS 处理后都可以变成一个真正的杰作.这里分享的这组 Photoshop 教程可以帮助你学习到新的照片处理技术.你会发 ...
- 【移动适配】移动Web怎么做屏幕适配(一)
屏幕适配是一个很容易被忽略的问题,但对于精益求精的产品而言,是必不可少的.对于Web开发的求职者而言,也是一个必需要理解清楚的经典问题 首发于微信公众号(啃先生) 壹 | Fisrt 移动端适配的是 ...
- ie7下的javascript兼容
<a href="javascript:;" onclick="functionone();"></a> <script> ...
- sharepoint2013的最少下载策略概述(MDS)
该策略是 SharePoint 2013 中的一种新功能,通过在用户导航到新页面时仅发送差异内容来减少页面加载时间. 最少下载策略 (MDS) 是 SharePoint 2013 中的一种新技术,可减 ...
- ubuntu 搭建开发环境
一. 安装C/C++程序的开发环境 1. sudo apt-get install build-essential //安装主要编译工具 gcc, g++, make 2. sudo apt-get ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q49-Q51)
Question 49You are designing a SharePoint 2010 intranet site for a corporation. Your design must mee ...
- 多线程(pthread、NSThread、GCD)
pthread C语言编写 跨平台可移植 线程生命周期需要我们来管理 使用困难 NSThread 面向对象的 可直接操作线程对象 线程生命周期需要我们来管理 使用简单 资源互斥(@synchroniz ...
- Mysql查询按照某字段指定顺序排序
在项目当中用到Sphinx的时候,很多人遇到了这样的问题:使用mysql+Sphinx检索出了相关度的ID后,如何按照指定ID在Mysql中进行排序呢?这里是我在项目中的解决方法: 1 SELECT ...
- gitlab+gerrit+jenkins持续集成框架
1.持续集成之gitlab+gerrit+jenkins 1.1. GitLab 1.1.1. 简介 GitLab 是一个使用使用Ruby on Rails搭建的,用于仓库管理系统的开源项目.使用Gi ...
- 关于学习javascript的一些建议
有被朋友或同事问到过,要如何学习前端技术,他们大多是已经掌握其他语言的程序员,或是计算机相关专业的在校生. 每次被问到,总要组织回忆一番,本着DRY原则,我还是根据我学习javascript(下文都简 ...