Last active
April 6, 2016 09:30
-
-
Save x43x61x69/c22a5d9797eba9872a7b to your computer and use it in GitHub Desktop.
Invert Binary Tree - https://twitter.com/mxcl/status/608682016205344768
This file contains 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. | |
* struct TreeNode { | |
* int val; | |
* struct TreeNode *left; | |
* struct TreeNode *right; | |
* }; | |
*/ | |
struct TreeNode* invertTree(struct TreeNode* root) { | |
if (root) { | |
root->left = invertTree(root->left), root->right = invertTree(root->right); | |
const struct TreeNode* tmp = root->left; | |
root->left = root->right; | |
root->right = tmp; | |
} | |
return root; | |
} |
This file contains 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. | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
TreeNode* invertTree(TreeNode* root) { | |
if (root) { | |
root->left = invertTree(root->left), root->right = invertTree(root->right); | |
std::swap(root->left, root->right); | |
} | |
return root; | |
} | |
}; |
This file contains 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. | |
* public class TreeNode { | |
* int val; | |
* TreeNode left; | |
* TreeNode right; | |
* TreeNode(int x) { val = x; } | |
* } | |
*/ | |
public class Solution { | |
public TreeNode invertTree(TreeNode root) { | |
if (root != null) { | |
root.left = invertTree(root.left); | |
root.right = invertTree(root.right); | |
TreeNode tmp = root.left; | |
root.left = root.right; | |
root.right = tmp; | |
} | |
return root; | |
} | |
} |
This file contains 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: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
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 |
This file contains 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 singly-linked list. | |
* class TreeNode { | |
* var val: Int? | |
* var left: TreeNode? | |
* var right: TreeNode? | |
* | |
* init(val aVal: Int?, left aLeft: TreeNode? = nil, right aRight: TreeNode? = nil) { | |
* val = aVal | |
* left = aLeft | |
* right = aRight | |
* } | |
* } | |
*/ | |
func invertTree(root: TreeNode?) -> TreeNode? { | |
if root != nil { | |
(root!.right, root!.left) = (invertTree(root!.left), invertTree(root!.right)) | |
} | |
return root | |
} | |
// invertTree(tree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment