Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Last active August 5, 2017 03:27
Show Gist options
  • Save cixuuz/edf4e8d73cadea873bac7403d4c7411a to your computer and use it in GitHub Desktop.
Save cixuuz/edf4e8d73cadea873bac7403d4c7411a to your computer and use it in GitHub Desktop.
[226 Invert Binary Tree] #leetcode
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root is None:
return None
root.right, root.left = self.invertTree(root.left), self.invertTree(root.right)
return root
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode node = invertTree(root.right);
root.right = invertTree(root.left);
root.left = node;
return root;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment