<LeetCode OJ> Invert Binary Tree【226】
2015-12-28 20:01
381 查看
226. Invert Binary Tree
My SubmissionsQuestion
Total Accepted: 57653 Total
Submissions: 136144 Difficulty: Easy
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.
Subscribe to see which companies asked this question
Hide Tags
Tree
/** * 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){ return NULL; }else{ //这样做将:树的底层先被真正交换,然后其上一层才做反转 TreeNode* newleft = invertTree(root->right); TreeNode* newright = invertTree(root->left); root->left = newleft; root->right = newright; return root; } } };
参考资源:
【1】博客地址,http://www.cnblogs.com/ganganloveu/p/4640933.html
相关文章推荐
- 音乐播放器
- 最短的基本字符串
- .NET学习(九)Timer控件的使用
- mybatis学习日记(一)1-预编译PreparedStatement以及jdbc缺点
- 编程练习 15.12.07~15.12.14
- 【codechef】s=abs(这部分数-剩下的数),求所有选取方式的s之和
- [IOS]NULL与nil的区别
- fiddler - 测试手机端软件
- 数据库之计算器
- Python知识点与应用
- 马哥Linux笔记-操作系统对主要三类硬件的管理和调度
- 编程练习 15.11.30~15.12.06
- STL----vector
- CMake cache
- 《从0到1》给创业者的8点干货
- CMake cache
- 分类算法精华理解
- wxPython入门中文版 (Getting Started with wxPython)
- 编程练习 期中考试
- tomcat如何配置环境变量