简体   繁体   English

计算二叉树的正确节点

[英]counting the right nodes of a binary tree

I want to count the right nodes of a binary tree, for example the following one:我想计算一棵二叉树的正确节点,例如以下一个:

    15
   /
10
   \
    14

so I made the following program:所以我做了以下程序:

public class NodeT {
    int elem;
    NodeT left;
    NodeT right;
    public NodeT(int elem){
        this.elem=elem;
        left=null;
        right=null;
    }
}


public class Tree {
    public NodeT createTree(){
        NodeT root=new NodeT(15);
        NodeT n1=new NodeT(10);
        NodeT n4=new NodeT(14);
        root.left=n1;
        n1.right=n4;
        return root;
    }
 public int countRight(NodeT root){
         if (root==null) return 0;    //version 1
         else{
             return 1+countRight(root.right);
         }
     }

I called my main program in the following way:我通过以下方式调用了我的主程序:

Tree tree=new Tree();
NodeT root=tree.createTree();
System.out.println(tree.countRight(root))

This code prints 1 as the correct answer, but I cannot understand why is this happening.此代码打印 1 作为正确答案,但我不明白为什么会发生这种情况。 For what I see the right branch of 15 is equal to null so the call to the recursive function countRight() should return 0 and print an incorrect answer.对于我看到的 15 的右分支等于 null,因此对递归函数 countRight() 的调用应返回 0 并打印不正确的答案。

I have seen other solutions and I found that for counting all the nodes they use solutions like the following:我看过其他解决方案,我发现为了计算所有节点,他们使用如下解决方案:

     static int n;
     public int countRight(NodeT root){   //version 2
         if (root==null) return 0;
         if (root.left!=null){
             n=countRight(root.left);
         }
         if (root.right!=null){
             n++;
             n=countRight(root.right);
         }
         return n;
     }

which seems more legit to me.这对我来说似乎更合法。 Could it be a case in which the first version fails?会不会是第一个版本失败的情况?

Thanks谢谢

A method like that should never use a static field, or any field for that matter. 这样的方法永远不要使用静态字段或与此相关的任何字段。

The task is to count the number of right nodes, which actually means to count the number of nodes where right is not null. 任务是计算正确的节点数,这实际上意味着计算right不为空的节点数。 You're not really counting the nodes, but the references to the nodes. 您并不是真正在计算节点,而是在对节点的引用。

It also means you have to scan all the nodes, which means that the method must traverse both left and right. 这也意味着您必须扫描所有节点,这意味着该方法必须同时左右移动。

Finally, the root node is by definition not a right node. 最后,根节点根据定义不是正确的节点。

public int countRight(NodeT node) {
    if (node == null)
        return 0;
    if (node.right == null)
        return countRight(node.left);
    return 1 + countRight(node.left) + countRight(node.right);
}

your first code will retrun 2 no way it returns 1 recursively calling 您的第一个代码将重新运行2,它不会以递归方式返回1

return 1+another deeper call until root.right==null 返回1 +另一个更深层次的调用,直到root.right == null

will result in 2 return based on your structure the tree you coded is different than the one you draw 将根据您的结构产生2个返回值,您所编码的树与您绘制的树不同

Suposing you can't debug, it would only return 0 if the parameter "NodeT root" was null. 假设您无法调试,则仅在参数“ NodeT root”为null时返回0。

I supose you want: 我想你要:

public int countRight(NodeT root, int currentDepth){
         if (root==null
            || root.right == null) // missing this comparison
             return currentDepth;
         else{
             return countRight(root.right, currentDepth++);
         }
     } 

Tree tree=new Tree();
NodeT root=tree.createTree();
System.out.println(tree.countRight(root), 0) // 0 is the begin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM