Invert a binary tree.

Example:

Input:

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

Output:

     4
/ \
7 2
/ \ / \
9 6 3 1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
return root;
}
}

[LC] 226. Invert Binary Tree的更多相关文章

  1. 226. Invert Binary Tree(C++)

    226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...

  2. 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 ...

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

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

  4. <LeetCode OJ> 226. Invert Binary Tree

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

  5. Python解Leetcode: 226. Invert Binary Tree

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

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

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

  7. Leetcode 226. Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 class Solution(object): ...

  8. Java for 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 wa ...

  9. (easy)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 ...

随机推荐

  1. this 深度面试题3

    window.val = 1; var obj = { val: 2, dbl: function () { this.val *= 2; val *= 2; console.log(val); co ...

  2. Maven--配置 Maven 从 Nexus 下载构件

    在 POM 中配置: <project> ... <repositories> <repository> <id>nexus</id> &l ...

  3. 冲刺期末阶段一<公文档案流转管理系统>

    今天下午的四节课要求自己完成公文流转管理系统,并规定时间看个人进程,相对来说我对增删改查掌握的不彻底,对项目的逻辑框架不太熟练,所以我感觉今天的进度有点慢.有待继续学习. 完成进度:1.分步骤先理清整 ...

  4. Java自学-泛型 泛型转型

    Java 中的子类泛型转型成父类泛型 步骤 1 : 对象转型 根据面向对象学习的知识,子类转父类 是一定可以成功的 package generic; import charactor.ADHero; ...

  5. java网络考试系统的设计与实现 jsp 源码

    开发环境: Windows操作系统开发工具:MyEclipse/Eclipse + JDK+ Tomcat + MySQL 数据库 项目简介: 网络考试系统主要用于实现高校在线考试,基本功能包括:自动 ...

  6. android设备内部添加apn信息

    由于工作原因今天需要给多台android设备中写入某张sim卡的apn相关信息,虽然可以通过sqlite命令写sql语句来写入到设备中,但设备一多起来就太低效了,所以在学习的过程中摸索着写了一个将ap ...

  7. Java基础三(2020.1.15)

    学习内容: 1.Java流程控制之循环结构 2.Java数组 3.Java方法 1.随机数:math.random()得到0-1之间的数     math.random()*10+1得到1-10之间的 ...

  8. 微信小程序2048开发进度(一)

    父亲是个体劳动者,他的兴趣就是下象棋,针对平时兴趣,我想做一款自己的2048小游戏,在慕课网观看了2048小游戏的讲解,以及关于开发小游戏的理论知识,对开发一款小游戏有了基本的认识.

  9. 在CentOS7上从源码编译安装redis,并做成服务程序

    1.安装编译的依赖环境 # 安装pcre开发包: yum install -y pcre-devel # 安装ssl功能需要openssl库 yum -y install openssl-devel ...

  10. QeePHP

    百度百科: https://baike.baidu.com/item/qeephp/8328612?fr=aladdin 官方地址: http://www.qeephp.cn/app/index.ph ...