[Leetcode] 226. Invert Binary Tree 解题报告
2017-06-26 10:41
453 查看
题目:
Invert a binary tree.
to
Trivia:
This problem was inspired by this
original tweet by Max
Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
思路:
没什么可说的。。。
代码:
/**
* 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 != NULL) {
TreeNode *node = root->left;
root->left = root->right;
root->right = node;
invertTree(root->left);
invertTree(root->right);
}
return root;
}
};
4000
Invert a binary tree.
4 / \ 2 7 / \ / \ 1 3 6 9
to
4 / \ 7 2 / \ / \ 9 6 3 1
Trivia:
This problem was inspired by this
original tweet by Max
Howell:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
思路:
没什么可说的。。。
代码:
/**
* 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 != NULL) {
TreeNode *node = root->left;
root->left = root->right;
root->right = node;
invertTree(root->left);
invertTree(root->right);
}
return root;
}
};
4000
相关文章推荐
- LeetCode 226.Invert Binary Tree 解题报告
- 【LeetCode】226. Invert Binary Tree 解题报告
- [leetcode] 226. Invert Binary Tree 解题报告
- 【LeetCode】226.Invert Binary Tree(Easy)解题报告
- 【LeetCode】Invert Binary Tree 解题报告
- LeetCode-Invert Binary Tree-解题报告
- [LeetCode226]Invert Binary Tree
- LeetCode-Binary Tree Right Side View-解题报告
- [LeetCode]226. Invert Binary Tree
- 【LeetCode】671. Second Minimum Node In a Binary Tree 解题报告
- LeetCode 104.Maximum Depth of Binary Tree 解题报告
- 【LeetCode】144 . Binary Tree Preorder Traversal 解题报告
- [Leetcode] 543. Diameter of Binary Tree 解题报告
- [Leetcode] 545. Boundary of Binary Tree 解题报告
- 【leetcode】【226】Invert Binary Tree
- LeetCode(199) Binary Tree Right Side View解题报告
- leetcode - 226. Invert Binary Tree
- ※ Leetcode - Tree - 226. Invert Binary Tree(反转二叉树 使用二级指针交换两个指针的地址)
- leetcode 226 Invert Binary Tree 翻转二叉树
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告