简体   繁体   English

在二叉树中对具有特定值的节点进行计数

[英]Counting nodes with a specific value in a binary tree

I am trying to come up with a way to traverse a binary tree recursively versus iteratively and count how many times a find a specific value. 我试图提出一种方法来递归地遍历遍历二叉树而不是迭代遍历二叉树,并计算找到特定值的次数。 One issue I'm running into is root in the first method. 我遇到的一个问题是第一种方法的根本原因。

Node inner class: 节点内部类:

private class Node {

    int data;
    Node root;
    Node left;
    Node right;
}

Recursive & helper method: 递归和辅助方法:

public int valCount(int val) {
    if (root != null) {
        return valCount(val, root);
    }
    return 0;
}

public int valCount(int val, Node root) {
    int cnt = 0;
    if (root.left != null) {
        if (root.left.data == val) {
            cnt++;
        }
        valCount(val, root.left);
    }


    if (root.right != null) {
        if (root.right.data == val) {
            cnt++;
        }
        valCount(val, root.right);
    }
    return cnt;
}

I haven't been able to test because of the root issue so I'm not entirely sure my output will be correct. 由于根本原因,我无法进行测试,因此我不确定我的输出是否正确。 So, the question begs to be asked... am I even on the right track?? 所以,这个问题很待问...我什至在正确的轨道上吗? Does my approach even make sense? 我的方法甚至有意义吗? Any help would be awesome. 任何帮助都是极好的。 Cheers! 干杯!

Each time valCount is called, a separate copy of the local variable cnt is created. 每次调用valCount ,都会创建局部变量cnt单独副本。 Thus when you call valCount for the root, this creates a variable cnt ; 因此,当您为根调用valCount时,这将创建一个变量cnt when valCount then calls itself for the left or right subtree, the new valCount has its own cnt , so when they increment cnt , they do not increment the cnt that the first valCount owns. valCount然后为左或右子树调用自身时,新的valCount具有其自己的 cnt ,因此,当它们递增cnt ,它们不会递增第一个valCount拥有的cnt This means that all the work done by valCount for the left and right subtrees is thrown away. 这意味着valCount对左右子树所做的所有工作都将被丢弃。

A simple way to fix this would be to notice that when you call valCount for the left or right subtree, the recursive call will return a value. 解决此问题的简单方法是注意,当您为左或右子树调用valCount时,递归调用将返回一个值。 You should use that value, instead of discarding the result: 您应该使用该值,而不是丢弃结果:

int leftCount = valCount(val, root.left);

and then do something with leftCount (I'll let you think about how to use it). 然后使用leftCount做一些事情(我会让您考虑如何使用它)。

EDIT: One more thing: valCount should look at root.data , but it shouldn't look at root.left.data or root.right.data . 编辑:另一件事: valCount应该查看root.data ,但不应查看root.left.dataroot.right.data Let the recursive calls do the work of looking at the data in the subtrees. 让递归调用完成查看子树中数据的工作。 That's how binary tree recursion often works. 这就是二叉树递归经常工作的方式。

You don't need "root" in your Node class. 您在Node类中不需要“ root”。 The "root" is the "this" object/pointer, right? “根”是“此”对象/指针,对不对? Pass another parameter "int[] cnt" to valCount. 将另一个参数“ int [] cnt”传递给valCount。 Instantiate cnt as int[1]. 将int实例化为int [1]。 Then in valCount do cnt[0]++. 然后在valCount中执行cnt [0] ++。 In the calling function after your recursion ends, print out cnt[0]. 递归结束后,在调用函数中,打印出cnt [0]。 And remove the cnt local variable. 并删除cnt局部变量。

each time you call the method recursively a new object of cnt will be created that takes the value 0. Which means each time you increment it the value is lost. 每次您递归调用该方法时,都会创建一个新的cnt对象,该对象的值为0。这意味着,每次递增该值时,该值都会丢失。 A good way to fix this is by placing cnt in the parameter of the method, and therefore each time you call it you pass the new value of cnt. 解决此问题的一种好方法是将cnt放在方法的参数中,因此,每次调用它时,都传递新的cnt值。

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

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