Last active
August 5, 2017 03:27
-
-
Save cixuuz/edf4e8d73cadea873bac7403d4c7411a to your computer and use it in GitHub Desktop.
[226 Invert Binary Tree] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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