【发布时间】:2019-01-06 02:03:22
【问题描述】:
我有以下代码用于计算树的深度:
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);
}
}
如果我给它输入[3, 9, 20, null, null, 15, 7],我希望res 等于3,因为它代表二叉树的深度。但是res 最终等于零。似乎从未改变。
有人知道为什么吗?
【问题讨论】:
标签: java recursion binary-tree depth-first-search pass-by-value