翻转一棵二叉树。

示例:

输入:

     4
/ \
2 7
/ \ / \
1 3 6 9

输出:

     4
/ \
7 2
/ \ / \
9 6 3 1

 思路

如果根节点存在,就交换两个子树的根节点,用递归,从下至上。、

解一:

auto tmp = invertTree(root->left);

将左子树整体 当作一个节点 交换。

最后返回根节点。

/**
* 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) return root;
auto temp=invertTree(root->left);
root->left=invertTree(root->right);
root->right=temp;
return root;
}
};

解二: 

只单单交换左右两个节点。

如果交换完后,左子树存在,就交换左子树,右子树存在,就交换右子树

/**
* 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) return root;
TreeNode *tmp = root->left;
root->left = root->right;
root->right = tmp;
if(root->left)invertTree(root->left);
if(root->right)invertTree(root->right);
return root; }
};

解三:

/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void Mirror(TreeNode *pRoot) { if(!pRoot)
return;
if(!pRoot->left&&!pRoot->right)
return; auto temp=pRoot->left;
pRoot->left=pRoot->right;
pRoot->right=temp; if(pRoot->left)
Mirror(pRoot->left);
if(pRoot->right)
Mirror(pRoot->right); }
};

第27题:Leetcode226: Invert Binary Tree反转二叉树的更多相关文章

  1. LeetCode Invert Binary Tree 反转二叉树

    思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...

  2. 【easy】226. Invert Binary Tree 反转二叉树

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  3. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  4. [LeetCode226]Invert Binary Tree

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 反转二叉树,左右儿子值交换 代码: / ...

  5. [刷题] 226 Invert Binary Tree

    要求 翻转一棵二叉树 实现 翻转左右子树,交换左右子树的根节点 1 class Solution { 2 public: 3 TreeNode* invertTree(TreeNode* root) ...

  6. leetcode 226 Invert Binary Tree 翻转二叉树

    大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...

  7. 226. Invert Binary Tree 翻转二叉树

    [抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...

  8. 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...

  9. [LeetCode] Invert Binary Tree 翻转二叉树

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...

随机推荐

  1. JMeter - 如何测试REST API / 微服务

    概述: 有许多方法和工具可用于测试REST API.当我需要测试REST API时,在查看了各种工具和选项之后,由于以下原因,我选择了JMeter. JMeter是免费和开源的. JMeter可以从C ...

  2. python_魔法方法(三):__str__()和__repr__()

    使用python的魔法方法和time模块定制一个计时器的类 1.用time模块里的localtime()方法获取时间2.time.localtime返回struct_time格式3.表现你的类:__s ...

  3. 小小粉丝度度熊 二分答案 + two pointer

    http://acm.hdu.edu.cn/showproblem.php?pid=6119 发现自己的two pointer能力超弱. 这题是合并时间后,二分答案. 可以知道对于每个时间区间,合法的 ...

  4. UiAutomator新建工程

    新建工程步骤: 1.打开Eclipse 2.新建一个java工程UiAutomatorDemo1,然后新建一个包com.hhb 3.选中java工程,右击新建文件夹,命名为libs,在D:\Andro ...

  5. list map set常用方法之list

    list 常用方法: 默认添加:list.add(e); 指定下标添加(添加后下标后的元素向后挪一位):list.add(index,e); 获得集合内元素个数:list.size(); 返回是否删除 ...

  6.  $(document).ready(function() { });

    l  $(document).ready(function() {   }); 初始化jsp

  7. Oracle单列函数

    --字符函数--1.ASCII 返回与指定的字符对应的十进制数;select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space fr ...

  8. OS进程同步与通信

    信号量机制 信号量用于互斥 P(S) 临界区 V(S) ----- P(S) 临界区 V(S) 生产者消费者: typedef int semaphore //信号量值设置为1就是互斥量 semaph ...

  9. 在java.ext.dirs中使用环境变量导致crontab执行不成功的问题及解决

    在java.ext.dirs中使用环境变量导致crontab执行不成功的问题及解决 Table of Contents 1. java.ext.dirs的使用和环境变量 2. 问题:在crontab中 ...

  10. hibernate课程 初探单表映射2-4 transaction简介

    1 hibernate是非自动提交.如果transaction不写的话,会只创建表结构而不插入语句.   如果不写transaction而想实现插入的功能的话,需要重写session的dowork方法 ...