Leetcode[226]-Invert Binary Tree
2015-06-12 17:32
260 查看
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.
递归法求解:
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.
递归法求解:
/** * 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) return NULL; invertNode(root); return root; } void invertNode(TreeNode *root){ if(root == NULL) return; if(!root->left && !root->right) return; TreeNode *tempNode=NULL; if(root->right) tempNode = root->right; if(root->left){ root->right = root->left; root->left = tempNode; }else{ root->left = tempNode; root->right = NULL; } invertNode(root->left); invertNode(root->right); } };
相关文章推荐
- 11 + 12
- 新浪微博产品管培生求职之路——方法论归纳与整理
- 深度学习笔记三:Softmax Regression
- 【北大青鸟视频学习】---过渡知识小总
- Java实现的堆排序算法
- 天声人語
- Windows10正式版将于7月29日正式发布
- 解决UISearchDisplayController搜索结果滚动时,顶部透明的问题
- LDAP学习笔记
- flexbuilder 4.6破解
- linux2.6 makefiles.txt学习及实例分析
- MapReduce编程之倒排索引
- [051] 微信公众平台开发教程第22篇-如何保证access_token长期有效
- centos7配置网卡
- background-size属性
- 机会主义和理想主义
- 机会主义和理想主义
- jQuery.extend 函数详解
- MySQL批量SQL插入性能优化
- 高斯坐标计算器App上线