尝试计算二叉树中的左节点

时间:2013-11-26 04:17:21

标签: binary-tree

我有一些案例通过这段代码,但其他人都失败了..这是我的逻辑还是if语句的顺序..请帮助..使用practiceitcs网站

public int countLeftNodes() {

    return countLeftNodes(overallRoot);
}

private int countLeftNodes(IntTreeNode root){

    int count = 0;

    if (root== null) return count;
    if (root.right == null && root.left == null)
        return count;

    if(root.right != null)  count += countLeftNodes(root.left);

    if (root.left!= null)
        count += 1 + countLeftNodes(root.left);

    return count;
}

0 个答案:

没有答案