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. JS-改变页面的颜色(三)

    需求:点击页面的按钮,改变页面的颜色 思路:一先画出最简单的页面,二想办法获取页面的body节点,三想办法修改body节点的背景颜色属性,四通过一个方法获取随机的颜色值           和第二个例 ...

  2. openldap加密传输sssd

    http://blog.father.gedow.net/2015/09/29/sssd-ldap-sudo/ yum -y install openldap-clients sssd authcon ...

  3. oracle client与ODAC的字符集

    1.pl/sql developer 9里检查客户端字符集与服务端是否一致 首选项,选项,检查客户机与服务器字符集是否匹配 2.Windows环境变量的修改即时生效 3.ODAC12安装后字符集的变化 ...

  4. 生成API文档的软件

    SandCastle:http://shfb.codeplex.com/SourceControl/latest#1952439 HTML Help:http://msdn.microsoft.com ...

  5. ASP.NET 操作Cookie详解 增加,修改,删除

    ASP.NET 操作Cookie详解 增加,修改,删除 Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份而储存在用户本地终端上的数据(通常经过加密).定义于RFC2109.它 ...

  6. Python2和Python3在windows下共存

    Python2.7 和 Python3不兼容,两种环境可能都会用到.ubuntu14.04中已经默认安装了这两个版本,在shell中输入python会自动进入Python2.7的交互环境,输入Pyth ...

  7. Windows程序设计(第五版)学习:第三章 窗口与消息

        第三章 窗口与消息 1,windows窗口过程:应用程序所创建的每一个窗口都有一个与之关联的窗口过程,用于处理传递给窗口的消息. 2,窗口依据窗口类来创建.窗口类标识了用于处理传递给窗口的消息 ...

  8. TListView Header重绘和高度设置

    TListView 的 Header 部分默认 BtnFace 颜色,高度也不能改变.我们可以通过编写一些代码来实现这些功能: 获得TListView 的Header 的句柄: TListView的H ...

  9. ORACLE数据库存储结构

    一.数据块 Oracle对数据库数据文件中的存储空间进行管理的单位是数据块.数据块是数据库中最小的(逻辑)数据单位,是最小的I/O单位.与数据块对应的,所有数据在操作系统级的最小物理存储单位是字节.每 ...

  10. Linux命令之reset - 终端屏幕混乱的终结者

    用途说明 reset命令是用来重新初始化终端的(terminal initialization).在有些情况,终端显示会混乱无比,比如不小心显示了一个二进制文件,以前我在不知道reset命令时,只好将 ...