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 ...
随机推荐
- ScrollMagic – 酷毙了!超炫的页面滚动交互效果
ScrollMagic 是一款 jQuery 插件,它让你可以像使用进度条一样使用滚动条.如果你想在特定的滚动位置开始一个动画,并且让动画同步滚动条的动作,或者把元素粘在一个特定的滚动位置,那么这款插 ...
- angular路由配置用法
在如今,很多事用于HTML5嵌套在webview,成为(伪)app,用a链接来跳转,简单页面来说的话,速度还是可以的,但是应用越来越多,是用不流畅,当然原生的app像安卓以及IOS来说的话,跳转是 ...
- go语言常用函数:cap
cap()函数返回的是数组切片分配的空间大小.//http://www.cnblogs.com/osfipin/. package main import "fmt" func m ...
- CloudStack安装
1.修改IP vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet ONBOOT=yes NM_CONTROLL ...
- UINavigationController和UIScrollView一起使用时导致UIScrollView位置偏移
iOS7之后,当UIViewController成为UINavigationController的控制视图,UIViewController的控制视图上的第一个子视图是UIScrollView,UIS ...
- 【代码笔记】iOS-点击一个button,出6个button
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> //加入头文件 #import "DCPathB ...
- 【读书笔记】iOS网络-理解错误源
考虑一个字节是如何从设备发往运程服务器以及如何从远程服务器将这个字节接收到设备,这个过程只需要几百毫秒时间,不过确要求网络设备都能正常工作才行.设备网络和网络互联的复杂性导致了分层网络的产生.分层网络 ...
- 【问题排查】StringIndexOutOfBoundsException
工作中遇到 java.lang.StringIndexOutOfBoundsException ,查看网上资料,总结如下 1.异常定义: Java API指出StringIndexOutOfBound ...
- MAC OS 系统使用心得
1.Windows快捷键在 mac os 里怎么调用 今天用teamviewer链接我在公司的电脑.我想调试程序,我程序默认F5是启动调试,但在mac os里,F5是调节屏幕亮度的. 这时候遇到快捷键 ...
- 混合使用TFVC和GIT配置库的优化方案
如果要选出最近几年在软件工程领域最热的技术,那毋庸置疑就是git了.作为分布式源代码管理(DVCS)的代表,git以其超快的操作,便捷的分支合并模型和P2P模式的代码分享模式让软件开发团队的很多复杂协 ...