假设某一对象的一个引用是A,如果想要在子函数f()里修改引用的值怎么办?不是修改A所指
对象的值,不使用全局变量。
感觉说的不清楚,上代码:
先是正确代码:
public class Solution {
TreeNode listTail = null;
public void inorder(TreeNode root){
if(root == null) return;
if(root.left != null){
inorder(root.left);
}
root.left = listTail;
if(listTail != null){
listTail.right = root;
}
listTail = root;
if(root.right != null){
inorder(root.right);
}
}
public TreeNode Convert(TreeNode root) {
if(root == null) return null;
inorder(root);
TreeNode head = listTail;
while(head.left != null){
head = head.left;
}
return head;
}
}
然后是错误代码:
public class Solution {
public void inorder(TreeNode root, TreeNode listTail){
if(root == null) return;
if(root.left != null){
inorder(root.left,listTail);
}
root.left = listTail;
if(listTail != null){
listTail.right = root;
}
listTail = root;
if(root.right != null){
inorder(root.right,listTail);
}
}
public TreeNode Convert(TreeNode root) {
if(root == null) return null;
TreeNode head = null;
inorder(root,head);
while(head != null && head.left != null){
head = head.left;
}
return head;
}
}
如果我想像错误代码那样写,调用这种形式的函数public void inorder(TreeNode root, TreeNode listTail,然后在函数内部修改listTial本身而不是其指向的对象,应该怎么做,但是不要用正确代码里使用成员变量的方法