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. MFC使用TRACKMOUSEEVENT触发mouseHover和mouseLeave

    为对话框添加WM_MOUSEHOVER或WM_MOUSELEAVE消息并不会响应.MFC需要特殊处理,其中一法就是使用TRACKMOUSEEVENT void CmfcDlgDlg::OnMouseM ...

  2. sql日期函数操作

    sql语句获取本周.本月.本年数据 SQL Serverselect * from [data] where  DATEPART(m,[date])=2 Accessselect * from [da ...

  3. MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题

    ///MVC4.0中项目发布遇到IE11时session存入URL中,导致记不住密码的问题,在webconfig中配置<system.web><authentication mode ...

  4. FMDBsqilte语句样式

    举例: @"create table student(stuid integer primary key autoincrement,name text,number integer,the ...

  5. openldap主机访问控制(基于ip)

    http://blog.oddbit.com/2013/07/22/generating-a-membero/ http://gsr-linux.blogspot.jp/2011/01/howto-o ...

  6. NHibernate系列文章十三:NHibernate批量更新

    摘要 对于批量插入和批量修改数据,通过设置NHibernate配置文件的BatchSize属性,可以大量减少NHibernate与数据库交互的次数. 1. Batch属性介绍 设置了BatchSize ...

  7. 轻量数据交换json,xml,ini

    json语法: object   {string:value,...} value   string/number/object/array/true/false/null array   value ...

  8. TListView的一些操作

    1,让滚动条滚动的API SetScrollPos int SetScrollPos(     _In_  HWND hWnd,     _In_  int nBar,     _In_  int n ...

  9. IOleItemContainer的接口定义

      IOleItemContainer的接口定义

  10. linux 错误处理

    linux程序设计中,有许多系统调用和函数会因为各种原因而失败.在失败时设置外部变量errno的值来指明失败原因.程序必须在函数报告出错之后立即检查errno变量,因为它可能被下一个函数调用所覆盖(外 ...