I did it in a recursive way. There is another iterative way to do it. I will come back at it later.

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/ // method 1: recursion
// 注意交换的是node,不是int value
public void invertBinaryTree(TreeNode root) {
if (root == null)
return; TreeNode temp = root.left;
root.left = root.right;
root.right = temp; invertBinaryTree(root.left);
invertBinaryTree(root.right);
}
}

Lintcode 175 Invert Binary Tree的更多相关文章

  1. 175. Invert Binary Tree【LintCode by java】

    Description Invert a binary tree. Example 1 1 / \ / \ 2 3 => 3 2 / \ 4 4    解题:题目要求讲二叉树的左子树和右子树对调 ...

  2. lintcode :Invert Binary Tree 翻转二叉树

    题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...

  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. 【07_226】Invert Binary Tree

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

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

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

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

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

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

  9. LeetCode—— Invert Binary Tree

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

随机推荐

  1. google快捷键,通过浏览器本身来查看

    今天玩google浏览器时发现一个不需要访问google就能查看其快捷键的方式,再此记录一下,以备后用. 1:打开Google浏览器 2:打开开发者工具,有三种方法 2-1:按F12快捷键 2-2:按 ...

  2. JS函数arguments数组获得实际传参数个数

    JS与PHP在函数传参方面有点不同,PHP形参与实参个数要匹配,而JS就灵活多了,可以随意传参,实参比形参少或多都不会报错. 实参比形参多不会报错 ? 1 2 3 4 5 function say(a ...

  3. 学习java第二天

    首先我们要知道,java是特分大小写的,基本上分为 类名 我们统一小写 如果是多级的 我们用点来隔开 比如 file.test.number1,类或者接口的话基本上大家都是首字母大写,常量全部大写,然 ...

  4. mongoDB 使用手册

      1.基本操作db.AddUser(username,password) 添加用户db.auth(usrename,password) 设置数据库连接验证db.cloneDataBase(fromh ...

  5. PHP面向对象的标准

    (1)所有数据都应该隐藏在所在的类的内部. (2)类的使用者必须依赖类的共有接口,但类不能依赖它的使用者. (3)尽量减少类的协议中的消息. (4)实现所有类都理解的最基本公有接口[例如,拷贝操作(深 ...

  6. 关于CacheLookup一个有趣的问题

    今天写一个与其他系统进行物料同步的接口,通过COM Business Connector调用Axapta3.0的方法将数据插入到物料表中,中间发生异常,事务回滚,再次调用的时候提示刚刚发生异常的物料已 ...

  7. JavaWEB域对象

    PageContext: ServletRequest: HttpSession: ServletContext: void setAttribute(String name, Object valu ...

  8. python sort和sorted的区别以及使用方法

    iteralbe指的是能够一次返回它的一个成员的对象.iterable主要包括3类: 第一类是所有的序列类型,比如list(列表).str(字符串).tuple(元组). 第二类是一些非序列类型,比如 ...

  9. mysql convert

    SELECT id,boshidianshu,boshidianshu_shuzi,CONVERT(REPLACE(boshidianshu, '个', ''),SIGNED) aaa from lg ...

  10. java.lang.UnsupportedClassVersionError: xxx/xxxClass : Unsupported major.minor version 51.0

    完全参考自 http://www.cnblogs.com/xing901022/p/4172410.html 这种错误的全部报错信息: 1 java.lang.UnsupportedClassVers ...