简体   繁体   English

为什么“ res”在我的DFS功能中没有改变?

[英]Why “res” does not change in my DFS function?

I have the following code for computing the depth of a tree: 我有以下代码用于计算树的深度:

class Solution {
    public int maxDepth(TreeNode root) {
        int res = 0;
        DFS(root, res, 1);
        return res;
    }
    private void DFS(TreeNode root, int res, int curDepth) {
        if (root == null) {
            return; 
        }

        if (curDepth > res) {
            res = curDepth;
        }
        DFS(root.left, res, curDepth + 1);
        DFS(root.right, res, curDepth + 1);
    }
}

If I give it the input [3, 9, 20, null, null, 15, 7] , I'd expect to have res equal 3, as it represents the depth of the binary tree. 如果我给它输入[3, 9, 20, null, null, 15, 7] ,我期望res等于3,因为它代表二叉树的深度。 But res ends up being equal to zero. 但是res最终等于零。 It seems that it never changed. 看来它从未改变。

Does anyone know why? 有人知道为什么吗?

Remember that in the language you're using (I think it's Java?) that parameters to functions are passed by value . 请记住,在您使用的语言(我认为是Java?)中,函数的参数是通过value传递的 That means that when you write 这意味着当你写

DFS(root, res, 1)

you're not actually taking the variable res and handing it off to the DFS function to modify. 您实际上并没有使用变量res并将其交给DFS函数进行修改。 Instead, you're saying "make a copy of whatever value happens to be stored in the variable called res , then hand that copy over to DFS ." 而是说“复制碰巧存储在名为res的变量中的任何值,然后将该副本交给DFS

The method DFS has a parameter called res , but the fact that it's called res and that you have your own variable called res up in maxDepth doesn't mean that those values are linked. DFS方法具有一个名为res的参数,但是在maxDepth其称为res以及您拥有自己的名为res的变量的maxDepth并不意味着这些值已链接。 As an example, I once taught I class with six students in it all named Zoe (true story!) Although all of them were named Zoe, they were definitely not the same person as one another! 举例来说,我曾经教过我的班级,里面有六个学生,全都叫佐​​伊(真实的故事!)。尽管他们全都叫佐伊,但他们绝对不是同一个人! I couldn't talk to one person name Zoe and expect all the other Zoes to have any inkling of what we talked about. 我无法与一个叫佐伊的人交谈,并且期望所有其他佐斯都对我们所说的有所了解。 In the same way, your res variable in maxDepth is completely separate from the res variable in DFS . 以同样的方式,你的res在变maxDepth是从完全独立的res变量DFS They coincidentally have the same first name, but they're not the same person. 他们碰巧有相同的名字,但他们不是同一个人。

There are a number of ways you could fix this, and probably the best way to do this would be to have DFS return a value to you, and then write your code like this: 您可以通过多种方法来解决此问题,并且可能最好的方法是让DFS向您返回值,然后像这样编写代码:

public int maxDepth(TreeNode root) {
    return DFS(root, 1);
}

private int DFS(TreeNode root, int currDepth) {
    // For you to figure out!
}

Returning a value from a function is the preferred way to get information out of one method and into another. 从函数返回值是使信息从一种方法获取并进入另一种方法的首选方法。 So now what you'll need to do is think about what value you'd like to return from DFS and what updates you'll need to make to it to get that information to propagate properly. 因此,现在您需要做的就是考虑要从DFS返回什么值,以及需要进行哪些更新才能使信息正确传播。

If you want to calculate the height of a node you need simply to add one to the maximum of the height of the left and right tree. 如果要计算节点的高度,只需在左侧和右侧树的最大高度中加一个即可。 If a node does not exist it has no height. 如果节点不存在,则它没有高度。

private int height(TreeNode node) {
    if (node == null) {
        return 0;
    }
    return 1 + Math.max(height(node.left), height(node.right));
}

The reason for your solution not to work is that the value of res is passed by value like templatetypedef said. 您的解决方案不起作用的原因是res的值是通过诸如templatetypedef之类的值传递的。

Actually you do not need a parameter with the depth to solve this problem. 实际上,您不需要深度参数即可解决此问题。

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

相关问题 为什么我的递归 dfs 函数返回错误的值? - Why is my recursive dfs function returning the wrong value? 通过JNI在Java代码中运行EGL函数时,为什么我对EGL函数的调用会发生变化? - Why does my call to an EGL function change when running it in Java code through JNI? 为什么我的SwitchPreference在方向上发生故障? - Why does my SwitchPreference glitch on orientation change? 为什么我的控制器中的保存功能不起作用? - Why the function save in my controller does not work? 为什么我的 firestore 函数向我的应用程序返回 null? - Why does my firestore function return null to my app? 为什么我的函数始终无法通过JUnit测试? - Why does my function keep failing my JUnit test? 为什么我的“为什么”循环中的“如果”语句不能更改我的一个对象? - Why does my 'if' statement in my 'why' loop not change one of my objects? LibGdx为什么Sprite draw()方法会更改我的X和Y - LibGdx Why does sprite draw() method change my X and Y 为什么我的源位置更改回原始位置? - Why does my source location change back to original? 为什么Joda时间会将输入字符串中的PM更改为AM? - Why does Joda time change the PM in my input string to AM?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM