// 二叉树节点定义
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. ssh方式请求gitlab需要密码解决方法

    问题:gitlab是使用docker安装的,配置好公钥私钥后,请求gitlab一直需要输入密码,而且这个密码输入什么都不对. 原因:后来发现是因为docker做了端口映射,如使用宿主机的10022映射 ...

  2. win10.64位wnmp-nginx1.14.0 + PHP 5. 6.36 + MySQL 5.5.59 环境配置搭建 结合Thinkphp3.2.3

    本文20%是原创,另外参考了这里https://blog.csdn.net/foolly/article/details/78963025 作者:CSDN 古雨蓝枫 和这里https://www.cn ...

  3. 事务回滚 try catch

    USE tempdb IF OBJECT_ID ('dbo.test') IS NOT NULL DROP TABLE dbo.test GO CREATE TABLE dbo.test ( id I ...

  4. Galaxy2D Game Engine 4.2

    Galaxy2D Game Engine 4.2 开发版  下载地址  D3DRender注意:1.下载后的压缩包请使用WinRar5.0打开.2.开发版包含了现在正在开发中的Galaxy2D游戏引擎 ...

  5. ef6.0+mysql配合使用的问题

    折腾了很久由于所用到的各种库版本问题:后来终于组合成了一个可用的:记录下各种库的版本 ef6.0 mysql5.5 mysql-connector-net-6.9.12.msi mysql-for-v ...

  6. Java进程&线程(一)

    Java进程&线程 程序:程序员写的代码,就是代码,不运行好像不会发生什么: 进程:一个进程可以理解为"运行的"一个程序,当我们启动一个java程序后,对应的jvm就会创建 ...

  7. C语言的split功能

    其它高级语言都有字符串的split功能,但C没有系统自带的,只能自己写一个了. void c_split(char *src, const char *separator, int maxlen, c ...

  8. C#中获取文件信息的代码

    如下的内容内容是关于C#中获取文件信息的内容,应该对大伙有一些好处. FileInfo fi = new FileInfo(@"C:file.txt"); if(fi.Exists ...

  9. Vue 封装可向左向右查看图片列表的组件

    <template> <div class="content-container"> <div class="content-contain ...

  10. Python 递归的练习

    递归的练习 递归的了解实例 # 定义一个类(num是需要给出的参数) # 一定要有临界值 # 要有递推的关系 def digui(num): # 打印num print('$'+str(num)) # ...