LeetCode_invert binary tree
2015-09-03 23:17
281 查看
原题链接:https://leetcode.com/problems/invert-binary-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) {
invert(root);
return root;
}
void invert(TreeNode* root)
{
if(!root)
return;
invert(root->left);
invert(root->right);
swap(root->left,root->right);
}
};
/**
* 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) {
invert(root);
return root;
}
void invert(TreeNode* root)
{
if(!root)
return;
invert(root->left);
invert(root->right);
swap(root->left,root->right);
}
};
相关文章推荐
- DataStructure-9-排序技术
- [leetcode-115]Distinct Subsequences(java)
- Cocos 3D功能初探学习笔记(1)---摄像机
- 自学QT之判断输入内容实现按钮可用或者不可用
- sizeof在32位和64位机器上的运行结果
- 关于OCTAVE
- Intent的显式与隐式用法
- HDU 1565【状压DP】
- Meteor:延迟补偿
- 怎么改变ADS1.2的字体大小
- 练练手--求串联电阻的阻值
- nodejs 浏览器弹窗下载图片 data:image/jpeg;base64示例
- Java泛型
- eclipse3.2安装 + 汉化 + 配置SWT Designer6.2.0
- 欧几里德算法
- redis运维常用命令
- 关于testlink重启后出现“1045 - Access denied for user 'my_db '@'localhost' (using password: YES)”的问题解决办法
- 【推荐】Navicat for MySQL:数据库管理工具
- 宁波Uber优步司机奖励政策(8月24日到8月30日)
- Combination Sum II