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)
{
TreeNode temp = root.left;
root.left = root.right;
root.right = temp; invertTree(root.left);
invertTree(root.right); return root;
}
return null;
}
}

LeeCode-Invert Binary Tree的更多相关文章

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

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

  2. 【07_226】Invert Binary Tree

    Invert Binary Tree Total Accepted: 54994 Total Submissions: 130742 Difficulty: Easy Invert a binary ...

  3. lc面试准备:Invert Binary Tree

    1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...

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

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

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

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

  7. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

  8. 【Invert Binary Tree】cpp

    题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solutio ...

  9. <LeetCode OJ> 226. Invert Binary Tree

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

  10. LeetCode_226. Invert Binary Tree

    226. Invert Binary Tree Easy Invert a binary tree. Example: Input: 4 / \ 2 7 / \ / \ 1 3 6 9 Output: ...

随机推荐

  1. UVa1399.Ancient Cipher

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. batch 批处理获取系统时间

    文件test.bat,内容命令如下: @echo off set filename=%,%-%,%-%,% %,%:%,%:%,% echo %filename% pause

  3. 谷歌Cartographer学习(2)-原理阐述与源码解析

    最近终于写完了毕业论文.想仔细研究下Cartographer.无奈自己学识有限,先看下网上大牛的解析,作一个汇总. 一.泡泡机器人原创专栏-cartographer理论及实践浅析 http://mp. ...

  4. PHP连接sql server 2005环境配置

    一.Windows下PHP连接SQLServer 2005 设定:安装的Windows操作系统(Win7 或XP均可.其它系统暂未測试),在C盘下:PHP的相关文件位于c:/PHP以下,其配置文件ph ...

  5. ECMAScript 6新特性介绍

    箭头函数 箭头函数使用=>语法来简化函数.在语句结构上和C#.Java 8 和 CoffeeScript相似,支持表达式和函数体. . =>`操作符左边为输入的參数.而右边则是进行的操作以 ...

  6. js中取session的值

    在js中貌似不能取session的值,我在后台设置的session一直拿不到,于是用间接的方式拿到session的值. 首先在jsp中嵌入java代码,用java设置一个变量来取session值,再在 ...

  7. iOS开发- 界面传值(1)-通知模式(广播)

    之后的几篇博客, 记录下不同界面间传值的经常使用办法. 这篇文章记录广播的方式. iOS的设计模式中,通知模式也是当中重要的模式之中的一个,Notification直译为通知,事实上本人认为叫做广播模 ...

  8. 为github帐号添加SSH keys

    为github帐号添加SSH keys 2012-05-26 00:05 34279人阅读 评论(6) 收藏 举报 ssh文本编辑gitvim工具up 使用git clone命令从github上同步g ...

  9. 初识_IOS-简易计算器-问题总结

    作为一个新手,只好拿所有开发者都写的不想写又没有太大难度的计算器来下手咯.比较细一点,耐心哟. 我们都知道,计算器首要任务就是计算,那我们就直接看成A+B=C,来进行分析了.对A,B,C三个对象进行分 ...

  10. EffectiveC#18--用IComparable和IComparer实现对象的顺序关系

    1..Net框架提供了接口来描述对象的顺序关系:IComparable 和IComparer. 2.IComparable 为类定义了自然顺序,实现IComparer接口的类可以描述其它可选的顺序 3 ...