title: Why does this not invert the binary tree?
body: <p>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.</p>
  <p>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.</p>
  <pre><code>    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);
          
  
  }
  }
  </code></pre>