zoukankan      html  css  js  c++  java
  • 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:

  • 相关阅读:
    asp.net c#中FCKeditor的详细配置及精简操作
    winform C#中Byte与String的转换方法,相互转换
    wp,wordpress博客怎样让首页的文章默认显示摘要
    vs2005 c#鼠标悬停高亮显示在gridview中
    我国CN域名一年减少600万个 全要求实名注册
    c# winform未能找到引用的组件“Excel”的解决办法
    asp.net c#中使用FCKeditor的方法,版本2.66
    C# 注册表操作类(完整版)winform
    c#如何打印picturebox里的图片,winform怎样打印picturebox里的图片
    imageready 如何保存为gif格式图片总结.
  • 原文地址:https://www.cnblogs.com/juliachenOnSoftware/p/4584849.html
Copyright © 2011-2022 走看看