为什么这不会颠倒二叉树?

huangapple go评论70阅读模式
英文:

Why does this not invert the binary tree?

问题

以下是翻译好的内容:

我不确定为什么这段反转二叉树的代码不起作用。在反转实际节点而不是值时,它可以正常工作,但在节点为null的情况下不起作用,这就是为什么我需要使用节点进行反转的原因。

您可以取消注释打印行以查看它是否命中了正确的值并且是否按预期更改了树,但它会返回原始数组。

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        invertBinaryTree(root.left, root.right);
        return root;
    }
    
    private static void invertBinaryTree(TreeNode l, TreeNode r) {
        if (l == null || r == null) {
            return;
        }
        
        TreeNode temp = l;
        l = r;
        r = temp;
            
        // System.out.println(l.val);
        // System.out.println(r.val);
            
        invertBinaryTree(l.left, r.right);
        invertBinaryTree(l.right, r.left);
    }
}
英文:

I'm not sure why this code to invert a binary tree does not work. It works when I invert the values instead of the actual nodes but in that case does not work if a node is null which is why I need to use the nodes for inversion.

You can uncomment the print lines to see it's hitting the correct values and changing the tree as it should be changed but it returns the original array.

    class Solution {
        public TreeNode invertTree(TreeNode root) {
            if(root == null) {
                return null;
            }
            invertBinaryTree(root.left, root.right);
            return root;
        }
        
    private static void invertBinaryTree(TreeNode l, TreeNode r) {
    if(l == null || r == null) {
        return;
    }
    
    TreeNode temp = l;
    l = r;
    r = temp;
        
//     System.out.println(l.val);
//     System.out.println(r.val);
        
    
    invertBinaryTree(l.left, r.right);
    invertBinaryTree(l.right, r.left);
        

}
}

答案1

得分: 1

Java使用“按值传递”,而不是“按引用传递”来传递方法参数。这意味着如果你将一个对象传递给一个方法,你可以在方法内部更新对象的成员变量,这些更改将反映在调用方法中。但是,如果你重写参数以引用不同的对象(就像通过更改l和r的值所做的那样),那么对该对象的引用不会在调用方法中更新。你可以将你的函数重写成这样:

private static void invertBinaryTree(TreeNode t) {
     TreeNode temp = t.left;
     t.left = t.right;
     t.right = temp;

     if(t.left != null)
         invertBinaryTree(t.left);
     if(t.right != null)
         invertBinaryTree(t.right);
}

然后你可以通过传递invertBinaryTree(root);来在树的顶部调用它。

英文:

Java uses "pass by value" and not "pass by reference" for method parameters. This means that if you pass an object to a method, you can update the object's member variables inside the method and the changes will be reflected in the calling method, however if you overwrite the parameter to refer to a different object (like you are doing by changing the values of l and r) the reference to that object won't be updated in the calling method. You can rewrite your function to something like this:

private static void invertBinaryTree(TreeNode t) {
     TreeNode temp = t.left;
     t.left = t.right;
     t.right = temp;

     if(t.left != null)
         invertBinaryTree(t.left);
     if(t.right != null)
         invertBinaryTree(t.right);
}

Then you would just call it on the top of the tree by passing invertBinaryTree(root);

huangapple
  • 本文由 发表于 2020年9月20日 06:02:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63973761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定