题目:

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.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return null;
}
if(root.left!=null){
root.left=invertTree(root.left);
}
if(root.right!=null){
root.right=invertTree(root.right);
}
TreeNode temp=root.right;
root.right=root.left;
root.left=temp;
return root;
}
}

Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]的更多相关文章

  1. LeetCode OJ 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. Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  3. <LeetCode OJ> 226. Invert Binary Tree

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

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

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

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

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

  7. Python解Leetcode: 226. Invert Binary Tree

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

  8. 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): ...

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

随机推荐

  1. Mac os 下的文件权限管理

    Mac os 下的文件权限管理 命令 ls -l -A 结果 -rw-r--r-- 1 user admin 2326156 4 12 15:24 adb 横线代表空许可.r代表只读,w代表写,x代表 ...

  2. 【BZOJ 1566】 1566: [NOI2009]管道取珠 (DP)

    1566: [NOI2009]管道取珠 Time Limit: 20 Sec  Memory Limit: 650 MBSubmit: 1659  Solved: 971 Description In ...

  3. FFTW3学习笔记2:FFTW(快速傅里叶变换)中文参考

    据说FFTW(Fastest Fourier Transform in the West)是世界上最快的FFT.为了详细了解FFTW以及为编程方便,特将用户手册看了一下,并结合手册制作了以下FFTW中 ...

  4. 什么是P问题、NP问题和NPC问题

    为了迎接我的期末考试,认真的看了一下关于NP完全性理论这一章,奈何课本上说的我怎么都看不懂,所以找了个博客认真研究了一下,同样贴出来分享给大家,大牛就是大牛,把问题说的很明白,看完后受益匪浅.其中有一 ...

  5. CodeForces - 1009D Relatively Prime Graph

    题面在这里! 直接暴力找点对就行了,可以证明gcd=1是比较密集的,所以复杂度略大于 O(N log N) #include<bits/stdc++.h> #define ll long ...

  6. iOS开发系列--视频处理

    MPMoviePlayerController 在iOS中播放视频可以使用MediaPlayer.framework种的MPMoviePlayerController类来完成,它支持本地视频和网络视频 ...

  7. 【原】配置Log4j,使得MyBatis打印出SQL语句

    [环境参数] JDK:jdk1.8.0_25 IDE:Eclipse Luna Servie Release 1 框架:Spring 4.1.5 + SpringMVC 4.1.5 + MyBatis ...

  8. Powerdesigner打开工程提示打印错误 解决方法

    在使用PowerDesigner打开工程时, 提示打印错误的问题,具体错误信息提示如下: 在您可以执行与打印机有关的任务(例如页面设置或打印一个文档)之前,您必须已经安装打印机.您想现在安装打印机么? ...

  9. IDA设置函数类型

    http://www.2cto.com/shouce/ida/1361.htm Action name: SetType 该命令允许你指定当前条目类型. 如果光标处在函数内部,那么函数类型将会被编辑, ...

  10. Struts2 include(包含)多个配置文件

    Struts 2自带有“包含文件”功能,包含多个Struts配置文件合并为一个单元. 单个Struts配置文件 让我们来看看一个糟糕的 Struts 2 配置示例. struts.xml <?x ...