[英]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.