繁体   English   中英

如何计算二叉树中“唯一子”节点的数量?

[英]How do I calculate the number of “only child”-nodes in a binary tree?

注意:这是与作业相关的,但我没有将其标记为因为“家庭作业”标签被标记为obselete(?)

使用以下实现二叉树的类...

class TreeNode
{
  private Object value; 
  private TreeNode left, right;

   public TreeNode(Object initValue)
  { 
     value = initValue; 
     left = null; 
     right = null; 
  }

   public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight)
  { 
     value = initValue; 
     left = initLeft; 
     right = initRight; 
  }

   public Object getValue()
  { 
     return value; 
  }

   public TreeNode getLeft() 
  { 
     return left; 
  }

   public TreeNode getRight() 
  { 
     return right; 
  }

   public void setValue(Object theNewValue) 
  { 
     value = theNewValue; 
  }

   public void setLeft(TreeNode theNewLeft) 
  { 
     left = theNewLeft;
  }

   public void setRight(TreeNode theNewRight)
  { 
     right = theNewRight;
  }

}

我需要计算二进制树中“只有子节点”的节点数,这被定义为一个节点,它没有源自其父节点的另一个节点。

这是我到目前为止:

public static int countOnlys(TreeNode t)
  {
     if(t == null)
         return 0;
     if(isAnOnlyChild(t))
         return 1;
     return countOnlys(t.getLeft()) + countOnlys(t.getRight());
  }

我不知道如何实现boolean方法isAnOnlyChild(TreeNode t)

有人可以帮我吗?

你非常接近并且遍历看起来很好但是在你的Treenode中你没有孩子和它的父母之间的联系。 因此,您无法说出一个左孩子是否存在兄弟姐妹(右孩子)。

您可以拥有父级Treenode(以及左侧和右侧),以便您可以检查给定节点的父级有多少个孩子。 或如ajp15243建议的那样,改用一种检查给定节点有多少个子节点的方法。

后者的一些伪代码:

//we still need to check if that only child has its own children
if hasOnlyChild(t) 
      return 1 + checkOnlys(left) + checkOnlys(right)
else 
      return checkOnlys(left) + checkOnlys(right)

正如您已经注意到的,一种解决方案是计算只有一个孩子的父母的数量。 这应该工作:

public static int countOnlys(TreeNode t)
  {
     if(t == null || numberOfChildren(t)==0){
         return 0;
     }
     if(numberOfChildren(t)==1){
         return 1+ countOnlys(t.getLeft()) + countOnlys(t.getRight());
     }
         if(numberOfChildren(t)==2 ){
         return countOnlys(t.getLeft()) + countOnlys(t.getRight());
    }
         return 0;
  }

public static int numberOfChildren (TreeNode t){
    int count = 0;
    if(t.getLeft() != null ) count++;
    if(t.getRight() != null) count++;       
    return count;   
    }

如果其中一个子项非空(这意味着它的一个子项为空),则父项具有唯一的子项:

((t.getLeft() == null || t.getRight() == null)) && !(t.getLeft() == null && t.getRight() == null)

但是,当递归代码遍历树时,您无需测试该节点。 (这类似于“访客”模式。)您要做的是在您坐在父母的陪伴下测试唯一的孩子。 它实际上是一个逻辑异或 - 测试,因为只有一个子节点需要非null才能检测到该节点只有一个子节点。

所以算法是

  1. 访问树中的每个节点。
  2. 如果节点只有一个孩子,请计算它

而已。 其余的都很好。

无论何时遍历二叉树,都要递归思考。 这应该工作。

public static int countOnlys(TreeNode t)
  {
     if(t == null)
         return 0;
     if (t.getLeft()==null&&t.getRight()==null)
         return 1;

     return countOnlys(t.getLeft())+countOnlys(t.getRight());

  }
public static int onlyChild(TreeNode t){
    int res = 0;
    if( t != null){
        // ^ means XOR 
        if(t.getLeft() == null ^ t.getRight() == null){
            res = 1;
        }

        res += onlyChild(t.getLeft()) + onlyChild(t.getRight()));
    }

    return res;
}
public int countNode(Node root) {

         if(root == null)
             return 0;

         if(root.leftChild == null && root.rightChild == null)
             return 0;
         if(root.leftChild == null || root.rightChild == null)
             return 1 + countNode(root.leftChild) + countNode(root.rightChild);
         else
             return countNode(root.leftChild) + countNode(root.rightChild);

     }

暂无
暂无

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

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