Skip to content

Instantly share code, notes, and snippets.

@mrjohannchang
Last active August 29, 2015 14:22
Show Gist options
  • Save mrjohannchang/4a6dca23dd1fdb455371 to your computer and use it in GitHub Desktop.
Save mrjohannchang/4a6dca23dd1fdb455371 to your computer and use it in GitHub Desktop.
class Solution:
# @param {TreeNode} root
# @return {TreeNode}
def invertTree(self, root):
if root:
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
return root
@yongjhih
Copy link

public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) return root;
        return swap(invertTree(root.left), invertTree(root.right), root);
    }

    TreeNode swap(TreeNode left, TreeNode right, TreeNode root) {
        root.left = right;
        root.right = left;
        return root;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment