226. Invert Binary Tree

Invert a binary tree.

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

to

     4
/ \
7 2
/ \ / \
9 6 3 1 代码实现
 /**
* 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) return root;
if(root->left!=nullptr||root->right!=nullptr)
{
swap(root->left,root->right);
root->left=invertTree(root->left);
root->right=invertTree(root->right);
}
return root;
}
};

226. Invert Binary Tree(C++)的更多相关文章

  1. LeetCode 226. 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 was ...

  2. LeetCode 226 Invert Binary Tree(转换二叉树)

    翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNod ...

  3. LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree

    258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...

  4. <LeetCode OJ> 226. Invert Binary Tree

    226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...

  5. C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...

  6. Python解Leetcode: 226. Invert Binary Tree

    leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...

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

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

  8. LeetCode OJ: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 was ...

  9. 【一天一道LeetCode】#226. Invert Binary Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...

随机推荐

  1. linux配置Java(JDK)环境变量

    本帖最后由 zhai 于 2013-11-19 23:00 编辑 1.下载jdk Oracle官方下载地址:http://www.oracle.com/technetwork/java/javase/ ...

  2. 【Mongous】write after end

    执行1(---) 执行2(----) 完成1(POST) 执行3(---)

  3. SqlServer:CTE函数处理递归(WITH语法)

    我们在做分类处理的时候,总会遇到递归的处理,比如说地区就是一个例子,中国--北京--西城区,我们可以把这样的信息存储在一个数据表中,用ParentID区分根节点和叶子节点.假如我们要做导航,得到了”西 ...

  4. HDOJ/HDU 1073 Online Judge(字符串处理~)

    Problem Description Ignatius is building an Online Judge, now he has worked out all the problems exc ...

  5. UVA 11021 Tribles(递推+概率)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=33059 [思路] 递推+概率. 设f[i]表示一只Tribble经 ...

  6. bzoj 1013 [JSOI2008]球形空间产生器sphere(高斯消元)

    1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3584  Solved: 1863[Subm ...

  7. (转载)绿色版Mysql的安装配置

    本文出自于:http://johnnyhg.javaeye.com/blog/245544 一.下载MySQL http://www.mysql.org/downloads 我下载的是mysql-no ...

  8. Linux process state codes

    Here are the different values that the s, stat and state output specifiers (header "STAT" ...

  9. flexpaper 与js 交互

    flash 代码//写到要响应的方法体中import flash.external.ExternalInterface;ExternalInterface.call("alert" ...

  10. Action

    学习Action的几个内容 1.实现一个Action的最常用方式: 从ActionSupport继承     链接 2.Action配置 DMI动态方法调用 ! 通配符配置 * {1} {2} … * ...