翻转二叉树
来源:互联网 发布:js object clone 编辑:程序博客网 时间:2024/06/01 07:24
1.问题描述
翻转一棵二叉树
样例
1 1 / \ / \2 3 => 3 2 / \ 4 4
2.解题思路
翻转就是把右节点赋给左节点,把左节点赋给右节点,然后递归所有节点,进行翻转操作。
3.代码实现
/**
* 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
TreeNode *temp;
if(root==NULL) return;
else
{
temp=root->left;
root->left=root->right;
root->right=temp;
invertBinaryTree(root->left);
invertBinaryTree(root->right);
}
}
};
4.感想
注意判空后,先赋值,后遍历。
- C++二叉树翻转
- 翻转二叉树
- 翻转二叉树
- 题目:翻转二叉树
- LintCode 翻转二叉树
- LinkCode-翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- LintCode_175_翻转二叉树
- 二叉树翻转
- lintcode ----翻转二叉树
- lintcode,翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- LintCode-翻转二叉树
- bzoj 4827: [Hnoi2017]礼物 fft
- Python学习第一弹——Python环境搭建
- Linux学习篇之~unit14
- 动态规划练习--05(吃糖果)
- fl2440添加SD卡驱动
- 翻转二叉树
- jzoj P1667 【AHOI2009】中国象棋
- JAVA:环链表(判断单链表是否存在环)
- 谷歌Android即将跨入8.0时代
- 先序序列和中序序列构造二叉树,中序序列和后序序列构造二叉树
- joda-time的使用
- 怎样才能算得上是技术牛人?
- QT下载
- ubuntu14.04 编译安装OpenCV 3.2