// 二叉树节点定义
public class BinaryTreefanzhuan {

class TreeNode{
  int value;
  TreeNode left;
  TreeNode right;
}

// 递归
public static TreeNode invertNode(TreeNode root){
  if (root == null) return null;
   TreeNode temp = root.left;
  root.left = invertNode(root.right);
  root.right = invertNode(temp);
  return root;
}

// 非递归
//交换左右节点后,将这个两个节点放入队列,继续下一层交换
public static TreeNode invertNode2(TreeNode root){
  if (root == null) return null;
  Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();
  while(!nodeQueue.isEmpty()){
    TreeNode current = nodeQueue.poll();
    TreeNode temp = current.left;
    current.left = current.right;
    current.right = temp;
    if (current.left != null) nodeQueue.add(current.left);
    if (current.right != null) nodeQueue.add(current.right);
    }
  return root;
}

Java反转二叉树的更多相关文章

  1. LeetCode OJ:Invert Binary Tree(反转二叉树)

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  2. leetCode之旅(12)-反转二叉树

    背景描述 Homebrew 是 OS X 平台上的包管理工具.用其官网的话说就是: the missing package manager for OS X | OS X 平台遗失的包管理器. 相信在 ...

  3. leetCode题解之反转二叉树

    1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...

  4. java创建二叉树并实现非递归中序遍历二叉树

    java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...

  5. Java实现二叉树及相关遍历方式

    Java实现二叉树及相关遍历方式 在计算机科学中.二叉树是每一个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(r ...

  6. A1102 | 反转二叉树

    #include <stdio.h> #include <memory.h> #include <math.h> #include <string> # ...

  7. 数据结构(5):Java实现二叉树

    二叉树图: package com.test.Sort; import java.util.ArrayList; import java.util.LinkedList; public class B ...

  8. java实现二叉树的Node节点定义手撕8种遍历(一遍过)

    java实现二叉树的Node节点定义手撕8种遍历(一遍过) 用java的思想和程序从最基本的怎么将一个int型的数组变成Node树状结构说起,再到递归前序遍历,递归中序遍历,递归后序遍历,非递归前序遍 ...

  9. LeetCode 226. Invert Binary Tree (反转二叉树)

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

随机推荐

  1. [蓝桥杯]PREV-26.历届试题_最大子阵

    问题描述 给定一个n*m的矩阵A,求A中的一个非空子矩阵,使这个子矩阵中的元素和最大. 其中,A的子矩阵指在A中行和列均连续的一块. 输入格式 输入的第一行包含两个整数n, m,分别表示矩阵A的行数和 ...

  2. python中Multiprocessing

    import multiprocessing as mp #该函数不能有返回值,如果需要则应该将值放在queue中 def job(a,b): print('aaaa') if __name__ == ...

  3. Linux 学习目录

    1 VIM 快捷键

  4. NPOI helper

    using NPOI.HSSF.UserModel; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; ...

  5. 完成端口IOCP详解

    修改自: http://blog.csdn.net/piggyxp/article/details/6922277 ps: 原作者很厉害了, 把一个iocp模型讲解的这么形象,不过在实践过程中发现一些 ...

  6. 【转录组入门】6:reads计数

    作业要求: 实现这个功能的软件也很多,还是烦请大家先自己搜索几个教程,入门请统一用htseq-count,对每个样本都会输出一个表达量文件. 需要用脚本合并所有的样本为表达矩阵.参考:生信编程直播第四 ...

  7. 记一次bond引起的网络故障

    本案中3个关键服务器 物理服务器:192.168.6.63,简称P,(Physical server) KVM-VM:192.168.6.150,是物理服务器P上的一个KVM虚机,简称VM NAS:外 ...

  8. Windows服务器

    知道了怎么装VMware workstation并且创建虚拟机装上了系统配好网络

  9. centos7,zabbix3.2通过zabbix_java_gateway监控jmx[java/tomcat]

    网络上很多教程也比较多和全了,但是自己做时候多多少少的坑备注下吧. 1,监控原理简单说一下,就是zabbix_server通过代理(zabbix_java_gateway)来获取agent端(tomc ...

  10. FIN_WAIT_2状态解释

    关于网络设备的FIN_WAIT_2状态解释出处:http://hi.baidu.com/netdemon1981/blog/item/584bfbb2aeb1d4acd9335ad9.html 在HT ...