LeetCode:Invert Binary Tree
2015-08-29 22:14
411 查看
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.
这道题有一个梗,homebrew的作者去Google面试就被问了这道题,结果他想了半天 没 想 出 来!哈哈哈class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL||root->left==NULL&&root->right==NULL){
return root;
}
TreeNode* temp=root->left;
root->left=root->right;
root->right=temp;
if(root->left){
invertTree(root->left);
}
if(root->right){
invertTree(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.
这道题有一个梗,homebrew的作者去Google面试就被问了这道题,结果他想了半天 没 想 出 来!哈哈哈class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==NULL||root->left==NULL&&root->right==NULL){
return root;
}
TreeNode* temp=root->left;
root->left=root->right;
root->right=temp;
if(root->left){
invertTree(root->left);
}
if(root->right){
invertTree(root->right);
}
return root;
}
};
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- 命令行快速技巧:如何定位一个文件
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- jquery+CSS实现的多级竖向展开树形TRee菜单效果
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解