LeetCode 刷题: 左右反转一个二叉树
2015-10-28 22:26
393 查看
/**
* 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)
{
TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
invertTree(root->left);
invertTree(root->right);
}
return root;
}
};
Invert a binary tree.
to
解法:
递归法:
* 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)
{
TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
invertTree(root->left);
invertTree(root->right);
}
return root;
}
};
Invert a binary tree.
4 / \ 2 7 / \ / \ 1 3 6 9
to
4 / \ 7 2 / \ / \ 9 6 3 1
解法:
递归法:
相关文章推荐
- wamp数据库mysql解决中文乱码问题方法
- 习题1-7 年份 解题报告
- lintCode_二分查找
- C++ 对象的内存布局(下)
- 数据结构-链表
- fibonacci的几种实现及尾递归
- WeTest 接口自动化测试框架
- C++ 对象的内存布局(上)
- javax.Swing
- mysql 1449 : The user specified as a definer ('root'@'%') does not exist ,mysql 赋给用户权限 grant all privileges on
- Apache apachectl命令
- Android 中获取系统时间
- 西方的一切
- 互联网项目开发过程中的测试分类
- 递归 尾递归
- 微信企业号开发(一)
- C++ 虚函数表解析
- 在html的css属性中,对p a span元素排版
- ASC||表
- c++虚继承对象的内存布局