简体   繁体   English

在递归调用期间,方法的参数如何存储在堆栈中?

[英]How does parameters of a method get stored in stack during a recursive call?

I was doing a leetcode question https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/ and I am confused on how parameters for a method get stored during a recursive call.我正在做一个 leetcode 问题https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/ ,我对在递归调用期间如何存储方法的参数感到困惑。

If a node gets visited, I wanted to save it's state that it was visited.如果一个节点被访问,我想保存它被访问的状态。 When I send two variables, when the stack is getting unwinded the updated variable values are being lost.当我发送两个变量时,当堆栈展开时,更新的变量值将丢失。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        boolean val1 = false, val2 = false;
        System.out.println("val1 - "+val1+" val2 - "+val2);
        System.out.println();

        TreeNode node = lca(root, p, q, val1, val2);
        System.out.println();
        System.out.println("val1 - "+val1+" val2 - "+val2);

        if(val1==true && val2==true)
            return node;
        return null;
    }
    
    private TreeNode lca(TreeNode root, TreeNode p, TreeNode q, boolean val1, boolean val2) {
        if(root==null) 
            return root;
        else if( p==root){
            val1 = true;
            System.out.println("val1 - "+val1+" val2 - "+val2);
            return root;
        } 
        else if( root==q){
            val2 = true;
            System.out.println("val1 - "+val1+" val2 - "+val2);
            return root;
        } 

        TreeNode left = lca(root.left, p, q, val1, val2);
        TreeNode right = lca(root.right, p, q, val1, val2);
        
        if(left==null && right==null) return null;
        else if(left==null) return right;
        else if(right==null) return left;
        else return root;
    }
}

Output -
val1 - false val2 - false

val1 - true val2 - false
val1 - false val2 - true

val1 - false val2 - false

But if I send an array and update values inside the array and pass the array itself as a parameter during recursion then the updated variable values are getting persisted.但是,如果我发送一个数组并更新数组内的值,并在递归期间将数组本身作为参数传递,那么更新的变量值将被持久化。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        boolean res[] = new boolean[2];
        //boolean val1 = false, val2 = false;
        System.out.println("val1 - "+res[0]+" val2 - "+res[1]);
        System.out.println();

        TreeNode node = lca(root, p, q, res);
        System.out.println();
        System.out.println("val1 - "+res[0]+" val2 - "+res[1]);

        if(res[0]==true && res[1]==true)
            return node;
        return null;
    }
    
    private TreeNode lca(TreeNode root, TreeNode p, TreeNode q, boolean res[]) {
        if(root==null) 
            return root;
        else if( p==root){
            res[0] = true;
            System.out.println("val1 - "+res[0]+" val2 - "+res[1]);
            return root;
        } 
        else if( root==q){
            res[1] = true;
            System.out.println("val1 - "+res[0]+" val2 - "+res[1]);
            return root;
        } 

        TreeNode left = lca(root.left, p, q, res);
        TreeNode right = lca(root.right, p, q, res);
        
        if(left==null && right==null) return null;
        else if(left==null) return right;
        else if(right==null) return left;
        else return root;
    }
}

Output - 

val1 - false val2 - false

val1 - true val2 - false
val1 - true val2 - true

val1 - true val2 - true

I feel like I am missing some basic knowledge here, can anyone please help me understand how the two code's are different and any material I can read to improve my knowledge on stack and recursion?我觉得我在这里缺少一些基本知识,谁能帮助我了解这两个代码的不同之处以及我可以阅读的任何材料以提高我对堆栈和递归的了解?

The issue has less to do with "recursion" and more to do with the function call itself.这个问题与“递归”关系不大,而与函数调用本身有关。

When you're calling foo(var1, var2) , the variables get passed by value .当您调用foo(var1, var2)时,变量按 value 传递 The changes you make inside the function block aren't propagated outside the function.您在功能块内所做的更改不会传播到功能之外。

When you pass the array, it gets passed by reference /pointer.当你传递数组时,它通过引用/指针传递。 Any modifications made to it are made to it's actual memory, so the changes gets retained/reflected even outside the function block.对其进行的任何修改都是对它的实际内存进行的,因此即使在功能块之外,更改也会保留/反映。

You can read more on the differences between passing by value and reference here .您可以在此处阅读有关按值传递和引用之间的区别的更多信息。

Edit:编辑:
Java is "always" pass-by-value because objects have references which are getting passed by value. Java“总是”按值传递,因为对象具有按值传递的引用。 This confusing terminology is clarified here in detail.这个令人困惑的术语在这里详细说明。

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

相关问题 在递归 java 方法中,每个方法调用都会导致一个新堆栈吗? - In a recursive java method does each method call result in a new stack? Java的递归调用堆栈需要占用多少内存? - How much memory does one stack of a recursive call in Java take? 由于递归方法调用,Java堆栈溢出 - Java stack overflow because of recursive method call SQLSyntaxErrorException 和如何使用 2 个参数调用 Get 方法? - SQLSyntaxErrorException and How to call a Get method with 2 parameters? 如何在递归堆栈上的同一数组上工作(或:如何将部分解决方案传递给方法的上一个/下一个递归调用) - How to work on the same array across the recursion stack (or: How to pass a partial solution to the previous/next recursive call to method) 如何摆脱递归方法调用的先前迭代? - How can one get rid of previous iteration of recursive method call? 如何调用带有参数的方法? - How to call a method with parameters? 如果方法的参数称为尾递归,则该方法是否具有一个方法调用? - Is a method that has a method call in one if its parameters called tail recursive? 为什么此方法会导致无限递归调用? - Why does this method cause an Infinite Recursive call? 递归方法和堆栈溢出 - Recursive method and stack overflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM