翻转二叉树
来源:互联网 发布:js改变display属性 编辑:程序博客网 时间:2024/06/07 21:33
题目:
翻转一棵二叉树
1 1 / \ / \2 3 => 3 2 / \ 4 4
思路:先交换左右子树,然后使用递归。
代码:
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void invertBinaryTree(TreeNode *root) {
// write your code here
if(root==NULL) return ;
TreeNode *T=root->left;
//int t=root->left->val;
root->left=root->right;
root->right=T;
if(root->left!=NULL) {invertBinaryTree(root->left);}
if(root->right!=NULL) {invertBinaryTree(root->right);}
}
};
感想:
这道题挺简单,不过当时上课我没听,没看课件自己写出来了,也是挺高兴的。
- C++二叉树翻转
- 翻转二叉树
- 翻转二叉树
- 题目:翻转二叉树
- LintCode 翻转二叉树
- LinkCode-翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- LintCode_175_翻转二叉树
- 二叉树翻转
- lintcode ----翻转二叉树
- lintcode,翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- LintCode-翻转二叉树
- SQLite数据库--案例:商品展示
- HAUTOJ-1267 ch追妹
- Android7.0 编译系统流程分析
- JDK 动态代理实现及原理解析
- centons7 mysql 安装及注意细节
- 翻转二叉树
- 一个MySql Sql 优化技巧分享
- C#中yield return用法分析
- java惰性初始化
- 文章标题
- jQuery闭包函数
- 使用D3制作统计图表、线性图表、水平柱状图表、饼图 -- (四)水平柱状图表+坐标轴
- 模板_tarjan强连通缩点算法
- 将二叉树拆成链表