Easy-题目4:226. Invert Binary Tree
来源:互联网 发布:unity3d 性能测试工具 编辑:程序博客网 时间:2024/04/24 08:52
题目原文:
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.
题目大意:
翻转一个二叉树。
琐事:
这个问题由HomeBrew软件的开发者Max Howell提出:
谷歌:虽然我们 90% 工程师都在用你写的软件(Homebrew),但你不能在白板上反转二叉树,所以滚蛋。
题目分析:
使用递归解法,对空二叉树直接退出,否则递归翻转左右子树并安到对应的位置上。
源码:(language:c)
struct TreeNode* invertTree(struct TreeNode* root) { if(!root) return NULL; struct TreeNode* temp = root->left; root->left = invertTree(root->right); root->right = invertTree(temp); return root;}
成绩:
0ms,beats0%.众数:0ms.100%
Cmershen的碎碎念:
一开始拿到此题时,想想连Homebrew作者都不会的题,我能会么。后来考虑了很多情况,例如叶子节点,只有一个孩子的节点等情况,代码写了很长。但接下来发现所有情况可以归结为空与非空两种情况,所以写出上述的ac代码应该是足够简洁且易理解了。
0 0
- Easy-题目4:226. Invert Binary Tree
- 226. Invert Binary Tree (Easy)
- 【leetcode】【Easy】【226. Invert Binary Tree】【tree】
- Invert Binary Tree(easy)
- 226. Invert Binary Tree [easy] (Python)
- Leetcode 226. Invert Binary Tree (Easy) (cpp)
- 226. Invert Binary Tree Difficult: Easy
- LeetCode 226. Invert Binary Tree (Easy)
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
- 226.leetcode Invert Binary Tree(easy)[二叉树 递归]
- [Leetcode 226, Easy] Invert Binary Tree
- <LeetCode><Easy>226Invert a binary tree
- Easy 226题 Invert Binary Tree
- LeetCode #226 - Invert Binary Tree - Easy
- 【Leetcode-easy-226】Invert Binary Tree
- 226.Invert Binary Tree
- 226. Invert Binary Tree
- 226. Invert Binary Tree
- Debug之路-1
- openstack policy 鉴权过程分析
- 职业生涯思考
- activiti 排他网管 :判断流程走向(十四)
- Easy-题目3:104. Maximum Depth of Binary Tree
- Easy-题目4:226. Invert Binary Tree
- XenServer6 虚拟机多vCPU配置方法
- 【NOIP2013模拟】归途与征程
- Easy-题目5:237. Delete Node in a Linked List
- 使用markdown编辑器
- 如何 操作Blob 大对象、映射派生属性 、映射组成关系
- java简单的StringBufferuilder和StringBuilder类练习
- Easy-题目6:283. Move Zeroes
- ping www.baidu.com 不通 开启DNS