Invert Binary Tree
2015-07-26 11:57
197 查看
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.
Google拒绝Max Howell:百分之90的工程师都用你写的软件,但你不能在白板上转换二叉树,滚蛋吧。
题目很简单,leetcode加这道题是因为Homebrew作者面试谷歌却没写出来被拒了,然后还在推上吐槽。我一开始居然想成了将二叉树对称,怎么想也想不出来。结果就是将二叉树的左右子树交换,递归swap左右子树就好了,代码如下:
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.
Google拒绝Max Howell:百分之90的工程师都用你写的软件,但你不能在白板上转换二叉树,滚蛋吧。
题目很简单,leetcode加这道题是因为Homebrew作者面试谷歌却没写出来被拒了,然后还在推上吐槽。我一开始居然想成了将二叉树对称,怎么想也想不出来。结果就是将二叉树的左右子树交换,递归swap左右子树就好了,代码如下:
/** * 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* IBT(TreeNode* root) { TreeNode* temp=root->left; root->left=root->right; root->right=temp; invertTree(root->left); invertTree(root->right); } TreeNode* invertTree(TreeNode* root) { TreeNode* result=root; if(root==NULL) return NULL; IBT(root); return result; } };
相关文章推荐
- Go Programming Blueprints 读书笔记(谈到了nsq/mgo处理数据持久化,但是业务逻辑不够复杂)
- 【面试加分项】java自定义注解之解析注解
- HDU 3033 组合背包变形 I love sneakers!
- Python网络编程:使用pexpect实现快速ssh连接
- php关于闭包的应用
- int数组与String数组互相转换和函数互相转换
- win7下删除多余引导
- Linux Shell介绍
- Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么?
- SQL删除重复数据方法
- linux设置程序开机启动
- 解决:eclipse的CTRL+space 被输入法占用的尴尬
- 线段树模板(区间更新)
- 记录学习的网站
- 在js文件中使用jstl标签
- Leetcode 19 Remove Nth Node From End of List
- 字符流
- 使用centos引导内核错误:kernel: pnp 00:0b: can't evaluate _CRS: 8
- 模版方法模式
- 使用centos引导内核错误:kernel: pnp 00:0b: can't evaluate _CRS: 8