public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return null;
TreeNode tmp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(tmp);
return root;
}
}
我不明白为什么上面的解决方案可行,但下面的解决方案不行。
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return null;
root.left = invertTree(root.right);
root.right = invertTree(root.left);
return root;
}
}
有人能详细解释一下为什么要有一个临时的TreeNode才能让程序正常工作吗?
https://stackoverflow.com/questions/41270660
复制