Description

Invert a binary tree.

Example

  1         1
/ \ / \
2 3 => 3 2
/ \
4 4

 

 解题:题目要求讲二叉树的左子树和右子树对调一下,用递归来做很简单:
 /**
* 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
*/
public void invertBinaryTree(TreeNode root) {
// write your code here
if(root == null)
return ;
TreeNode left = root.left;
TreeNode right = root.right;
root.left = right;
root.right = left;
invertBinaryTree(root.left);
invertBinaryTree(root.right);
}
}

非递归法:

 public class Solution {
public TreeNode invertTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
if(root!=null) q.offer(root);
while(!q.isEmpty()){
TreeNode curr = q.poll();
TreeNode tmp = curr.right;
curr.right = curr.left;
curr.left = tmp;
if(curr.left!=null) q.offer(curr.left);
if(curr.right!=null) q.offer(curr.right);
}
return root;
}
}

175. Invert Binary Tree【LintCode by java】的更多相关文章

  1. 155. Minimum Depth of Binary Tree【LintCode by java】

    Description Given a binary tree, find its minimum depth. The minimum depth is the number of nodes al ...

  2. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  3. Lintcode 175 Invert Binary Tree

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

  4. 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】

    Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...

  5. 375. Clone Binary Tree【LintCode java】

    Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...

  6. 501. Find Mode in Binary Search Tree【LeetCode by java】

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  7. 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】

    Big binary tree Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. LC 431. Encode N-ary Tree to Binary Tree 【lock,hard】

    Design an algorithm to encode an N-ary tree into a binary tree and decode the binary tree to get the ...

  9. 212. Space Replacement【LintCode by java】

    Description Write a method to replace all spaces in a string with %20. The string is given in a char ...

随机推荐

  1. 查询sqlserver 表结构呀

    SQL Server里查询表结构命令 对于SQL Server数据库有两种方法查询表结构 第一种方法 sp_help Accounts_Users     其中Accounts_Users 表示表名 ...

  2. 07JavaScript数据类型

    JavaScript 数据类型 值类型(基本类型):字符串(String).数字(Number).布尔(Boolean).对空(Null).未定义(Undefined).Symbol. 引用数据类型: ...

  3. 基于JQ的简版选项卡记录

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. ajaxSubmit 在ie9或360兼容中,form下是空的

    解决办法:在<head>....</head>中加入<meta http-equiv="X-UA-Compatible" content=" ...

  5. 企业IT架构转型之道 读后感

    放假三天,用部分时间阅读了企业IT架构转型之道这本书.第一遍潦草读完,就感觉收益颇多.这本书值得多读几遍,适合精度. 作为银行IT开发人员,在央企IT成本部门的大背景下,开发过程中遇到的诸多疑惑.困惑 ...

  6. MFC非模态添加进程控件方法一(线程方法)

    由于非模态对话框的自己没有消息循环,创建后无法进行消息处理.需要和父窗口共用消息循环.如果单独在子窗口进行控件由于自己没有单独的消息循环,更新是无法进行的. 如果在父窗口更新控件会造成程序假死.如以下 ...

  7. Qt上FFTW組件的编译与安裝

    Qt上FFTW組件的編譯安裝 FFTW是一個做頻譜非常實用的組件,本文講述在Windows和Linux兩個平臺使用FFTW組件.Windows下的的FFTW組件已經編譯好成爲dll文件,按照開發應用的 ...

  8. linux mint系统 cinnamon桌面 发大镜功能

    让我来告诉迷途中的你cinnamon桌面一个好用的功能. 选择设置 选择窗口 -> 选择行为 看那个窗口移动和调整大小的特殊键 Alt 好了按住alt在滑动滑轮 世界不一样了 对于小屏幕高分辨率 ...

  9. error: command 'aarch64-linux-gnu-gcc' failed with exit status 1

    使用jetson tx2安装tensorpack时报错: error: command 'aarch64-linux-gnu-gcc' failed with exit status 1 改正: 如果 ...

  10. 如何将github项目上传至gitlab

    一.修改远程分支关联 删除远程分支关联 将指向github的远程分支关联关系删除 git remote rm origin 添加新的远程分支关联 新的remote地址指向gitlab相应地址 git ...