```
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
//TreeNode left=root.left;
//TreeNode right=root.right;
if(root.left==null&&root.right==null){
return root;
}
TreeNode temp=root.left;
root.left=root.right;
root.right= temp;
if(root.left!=null)
invertTree(root.left);
if(root.right!=null)
invertTree(root.right);
return root;
}
}
```

why!!!!!!!!!!

```
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
TreeNode left=root.left;
TreeNode right=root.right;
if(left==null&&right==null){
return root;
}
TreeNode temp=left;
left=right;
right= temp;
if(left!=null)
invertTree(left);
if(right!=null)
invertTree(right);
return root;
}
}
```