LeetCode:226. Invert Binary Tree
2017-03-18 15:28
295 查看
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.
Subscribe to see which companies asked this question.
AC:
/**
* 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) {
invertTree(root->left);
invertTree(root->right);
swap(root->left, root->right);
}
return root;
}
};
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.
Subscribe to see which companies asked this question.
AC:
/**
* 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) {
invertTree(root->left);
invertTree(root->right);
swap(root->left, root->right);
}
return root;
}
};
相关文章推荐
- Leetcode 226:Invert a binary tree
- LeetCode 226. Invert Binary Tree 递归、指针交换 思考链表的交换
- LeetCode 226. Invert Binary Tree
- [leetcode]: 226. Invert Binary Tree
- LeetCode 226. Invert Binary Tree
- LeetCode 226 Invert Binary Tree(翻转二叉树)
- Leetcode 226 Invert Binary Tree
- 【LEETCODE】226-Invert Binary Tree
- LeetCode 226. Invert Binary Tree
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
- leetcode 226: Invert Binary Tree
- [LeetCode]226. Invert Binary Tree
- LeetCode-226. Invert Binary Tree
- 【LeetCode】226. Invert Binary Tree
- [leetcode-226]Invert Binary Tree(c++)
- leetcode(62).226. Invert Binary Tree
- Leetcode[226]-Invert Binary Tree
- 【LeetCode】226. Invert Binary Tree 解题报告
- Leetcode 226 Invert Binary Tree 反转二叉树
- Leetcode 226. Invert Binary Tree