【问题标题】:Why is recursion causing the count to return 2 instead of 4?为什么递归导致计数返回 2 而不是 4?
【发布时间】:2021-09-20 07:13:50
【问题描述】:

我正在尝试在 Leetcode 上做平衡树问题,只有当左子树的高度 - 右子树的高度 时才返回 true。 为什么左子树的深度应该返回 4 而返回 2?有什么我解释错了吗?我在底部附上了一张树的图片。

输入:[1,2,2,3,3,null,null,4,4,null,null,5,5]

输出:真(因为左子树返回 2 而不是 4)

预期输出:假

打印报表:

左子树:2

右子树:1

结果:1​​(左子树 - 右子树)

    /**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
    // 1 2 2 3 3 4 4
    // 
      
    if (root == null) return true;
    System.out.println("left subtree: " + findDepth(root.left,1));   
    System.out.println("right subtree: " + findDepth(root.right,1));   
    System.out.println("result: " + Math.abs(findDepth(root.left,1) - findDepth(root.right, 1)));
    if ( Math.abs(findDepth(root.left,1) - findDepth(root.right, 1)) <= 1) return true;
    return false;
    }
  
  public static int findDepth(TreeNode root, int count) {
    if (root == null) return count;
    if (root.left != null) {
       count++;
       findDepth(root.left, count);
    }
     if(root.left == null && root.right == null) return count;

    return count;
  }
    
} 

Image of Binary Tree

【问题讨论】:

  • this 回答你的问题了吗?
  • @Sweeper 基于调试器,计数达到 4 然后开始减少到 2。我不确定为什么在代码中的任何地方没有计数时它会减少。我想了解为什么递归会减少计数器。

标签: java binary-tree


【解决方案1】:

你得到 2 的原因是因为当你递归时,递归调用不会增加你传入的 count 变量。相反,它会增加它自己的副本Java is pass by value.

if (root.left != null) {
   count++;
   // the call below will not change "count" at all
   findDepth(root.left, count);
   // it will only change its own copy
}

因此,递归调用实际上什么都不做。你甚至没有使用它的返回值。它的返回值实际上是您希望将count 设置为的count 的修改副本,因此要解决此问题,请将count 设置为findDepth 的返回值:

value = findDepth(root.left, count);

这将为您提供预期的 4,但您的 findDepth 还应该检查正确的子树,并且可以通过其他方式进行改进。例如,您实际上并不需要 count 参数。

public static int findDepth(TreeNode root) {
    if (root == null) return 0;
   
    return 1 + Math.max(findDepth(root.left), findDepth(root.right));
}

【讨论】:

    【解决方案2】:

    因为你只是通过应用条件来计算findDepthmethof 中树左侧的深度

    if (root.left != null) {
       count++;
       findDepth(root.left, count);
    }
    

    你可以像这样修改你的方法

      public static int findDepth(TreeNode root, int count) {
        if (root == null) return count;
       
        return Math.max(findDepth(root.left, count+1),findDepth(root.right, count+1));
      }
    

    【讨论】:

    • if (root.left != null || root.right != null) { count++; findDepth(root.left, count); findDepth(root.right, count);为什么这不做任何事情?并感谢我想为您的评论点赞,但没有足够的声望点!
    • 谢谢...如果您从 findDepth 方法读取返回值,它将起作用,因为当您将值传递给函数时,method 内部发生的任何具有该值的事情都不会’不会被反射回来,因为它是 pass by value 而不是 pass by reference
    猜你喜欢
    • 1970-01-01
    • 2016-01-13
    • 2016-09-15
    • 2013-08-26
    • 2022-12-06
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多