简体   繁体   中英

a method to count the number of nodes with 2 children in Binary search tree

Thats the best I could come up but it still doesn't work cause it returns 1 even if there was more than one node that have two children.

int countTwoChildren(Node node)
{
    if(node==null) {
        return 0;
    }
    if(node.left!=null && node.right!=null) {
        return 1;
    }
    return countTwoChildren(node.left) + countTwoChildren(node.right);
}

Can anyone find any error in above piece of code?

One small thing missing:

int countTwoChildren(Node node)
{
    if(node==null) {
        return 0;
    }
    if(node.left!=null && node.right!=null) {
        return 1 + countTwoChildren(node.left) + countTwoChildren(node.right);
    }
    return countTwoChildren(node.left) + countTwoChildren(node.right);
}

Your problem is that if a node has two children, you don't descend down its own children. You should change the order of checks:

int countTwoChildren(Node node)
{
    int nc;

    if(node==null) {
        return 0;
    }

    nc = countTwoChildren(node.left) + countTwoChildren(node.right);
    if(node.left!=null && node.right!=null) {
        return nc++;
    }

    return nc;
}

Of course, this whole thing can be written in one line:

int countTwoChildren(Node node)
{
    return (node == null
            ? 0
            : countTwoChildren(node.left) + countTwoChildren(node.right) +
              (node.left!=null && node.right!=null ? 1 : 0));
}
int countTwoChildren(Node node)
{
    if (node == null)
        return 0;
    int here  = node.left != null && node.right != null ? 1 : 0;
    int left  = countTwoChildren(node.left);
    int right = countTwoChildren(node.right);
    return here + left + right;
}

Well all your missing is an else, like you have an if statement that checks if the node has both left & right links not null, but what if it's null,

You can simply add the else:

if(node.left!=null && node.right!=null) {
    return 1 + countTwoChildren(node.left) + countTwoChildren(node.right);
}else{
   return countTwoChildren(node.left) + countTwoChildren(node.right);
  }

Also you went wrong when you said if both left and right nodes are not null it will just return 1, you should continue traversing the tree to find the other by recursively calling the countTwoChildren for the left nodes and the right nodes respectively.

Your program will terminate the very first time if root has both left and right node so it will return back 1 , and will not go in recursive call . Here is the solution , hope it helps

public static int numberOfFullNode(TreeDemo root){
        if(root==null)
            return 0;
        else if(root.left!=null && root.right!=null)
            return 1+numberOfFullNode(root.left)+numberOfFullNode(root.right);
        else return 0;
    }

The question is already answered well,Just sharing the iterative solution for the same problem :

public static int findTheNumberOfFullNodesIterative(BTNode root) {

    int noOfFullNodes = 0;
    if (root == null) {
        return 0;
    }
    Queue<BTNode> q = new LinkedList<>();
    q.offer(root);

    while (!q.isEmpty()) {
        BTNode temp = q.poll();
        if (temp.getLeft() != null && temp.getRight() != null) {
            noOfFullNodes++;

        }
        if (temp.getLeft() != null) {
            q.offer(temp.getLeft());
        }
        if (temp.getRight() != null) {
            q.offer(temp.getRight());
        }
    }
    return noOfFullNodes;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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