简体   繁体   English

BST-计数带有左右子节点的节点

[英]BST - counting nodes with left and right children

As stated in the title, I'm trying to solve this problem by only counting nodes in a BST that have both left and right children. 如标题中所述,我试图通过仅计算BST中同时具有左右子节点的节点来解决此问题。 I'm struggling to think of the logic to solve this. 我正在努力思考解决这个问题的逻辑。

I thought of something like this. 我想到了这样的事情。

First, check if the root is null or if it has any null children. 首先,检查根是否为空或子级是否为空。 Next, traverse the tree going right and continue to check the children, increment a counter when the conditions are met. 接下来,遍历右转的树并继续检查子级,在满足条件时增加一个计数器。 But what happens when I reach the end node and need to go back to a node that had a left child to traverse? 但是,当我到达末端节点并需要返回到有一个左孩子要遍历的节点时会发生什么? I had a temp node to keep track of the most previous parent, but what about when I need to go up more than one level? 我有一个临时节点来跟踪最近的父级,但是当我需要上一层以上时该怎么办? I'm assuming the answer to this problem is to recursively solve it, but I don't even know where to begin. 我以为这个问题的答案是递归地解决它,但我什至不知道从哪里开始。

Here's what I have: 这是我所拥有的:

public int fullNodes() {
    int count = 0;
    Node n = root;
    Node temp = null;

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

    count++; //increment count, since the root has children on both sides

    temp = n; //hold the previous place
    n = n.right; //go right

    //Now what?

    return count;
}

I'm still struggling to think recursively when problem solving, in addition to my question, how do you learn to think recursively? 解决问题时,我仍在努力进行递归思考,除了我的问题之外,您如何学习递归思考? Just a ton of practice, or is there some tricks and tips that you use to solve problems? 只是大量的实践,还是您使用一些技巧来解决问题?

Rather than using a temp variable to hold the previous node -- which would only work for a depth of 1 -- call the same function on the child nodes . 在子节点上调用相同的函数,而不是使用temp变量来保存前一个节点(只能在深度为1的情况下使用)。

Recursive tree traversal might look something like this: 递归树遍历可能看起来像这样:

public int countSomething (Node node) {

    // Self;
    //   
    int result = 1;   // use your required logic here

    // Children;
    //    
    if (node.left != null)
        result += countSomething( node.left);
    if (node.right != null)
        result += countSomething( node.right);

    // done.
    return result;
}


// example usages
int treeTotal = countSomething( rootNode);
int subtreeTotal = countSomething( subtree);

The execution callstack will then hold recursive invocations of the function, each with their appropriate context. 然后,执行调用堆栈将保留函数的递归调用,每个调用都具有其适当的上下文。 When the top-level call returns, it will have summed the answer for the entire tree/ or subtree it was called on. 当顶级调用返回时,它将汇总被调用的整个树或子树的答案。

Put appropriate logic for your BST "node has both left & right children" in, instead of the constant 1. 为您的BST“节点同时具有左右两个子节点”输入适当的逻辑,而不是常量1。

First let us create representation of your Node class 首先让我们创建您的Node类的表示形式

class Node {
    public Node left;
    public Node right;
    public Node(){}
    public Node(Node left, Node right) {
        this.left = left;
        this.right = right;
    }
}

Then we write our recusrive function and client that uses your function 然后,我们编写我们的可恢复函数和使用您函数的客户端

public class Main {   

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

    public static void main(String[] args) {
        Node right = new Node();
        Node left = new Node();
        Node root = new Node(left, right);
        root.right = new Node(new Node(), new Node());
        System.out.println(countNodes(root));
    }

}

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

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