Java反转二叉树
// 二叉树节点定义
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反转二叉树的更多相关文章
- 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 ...
- leetCode之旅(12)-反转二叉树
背景描述 Homebrew 是 OS X 平台上的包管理工具.用其官网的话说就是: the missing package manager for OS X | OS X 平台遗失的包管理器. 相信在 ...
- leetCode题解之反转二叉树
1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...
- java创建二叉树并实现非递归中序遍历二叉树
java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...
- Java实现二叉树及相关遍历方式
Java实现二叉树及相关遍历方式 在计算机科学中.二叉树是每一个节点最多有两个子树的树结构.通常子树被称作"左子树"(left subtree)和"右子树"(r ...
- A1102 | 反转二叉树
#include <stdio.h> #include <memory.h> #include <math.h> #include <string> # ...
- 数据结构(5):Java实现二叉树
二叉树图: package com.test.Sort; import java.util.ArrayList; import java.util.LinkedList; public class B ...
- java实现二叉树的Node节点定义手撕8种遍历(一遍过)
java实现二叉树的Node节点定义手撕8种遍历(一遍过) 用java的思想和程序从最基本的怎么将一个int型的数组变成Node树状结构说起,再到递归前序遍历,递归中序遍历,递归后序遍历,非递归前序遍 ...
- 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 ...
随机推荐
- python调试pdb
开始调试 python3 -m pdb pdb.py break 或 b line_num 设置断点设置断点 continue 或 c继续执行程序 list 或 l查看当前行的代码段 step 或 s ...
- adb 常用命令-转载
转自:https://blog.csdn.net/suxing_ing/article/details/54907860 显示当前运行的全部模拟器:adb devices 获取序列号:adb get- ...
- struct2depth 记录
把效果图放在前面 03.28 handle_motion False architecture simple joint_encoder False depth_normalization ...
- celery定时任务
from celery import Celery from datetime import timedelta app = Celery('gx', broker='redis://localhos ...
- Pandas学习笔记(三)
(1)系列对象( Series)基本功能 编号 属性或方法 描述 1 axes 返回行轴标签列表. 2 dtype 返回对象的数据类型(dtype). 3 empty 如果系列为空,则返回True. ...
- 进程、线程与GIL全局解释器锁详解
进程与线程的关系: . 线程是最小的调度单位 . 进程是最小的管理单元 . 一个进程必须至少一个线程 . 没有线程,进程也就不复存在 线程特点: 线程的并发是利用cpu上下文的切换(是并发,不是并行) ...
- django中forms和modelform组件的区别
首先,我们来看看modelform的实现 model.py class Book(models.Model): title=models.CharField(max_length=32) price= ...
- Ubuntu忘记超级用户root密码,重新设置密码
Ubuntu版本:Ubuntu 16.04.3 LTS 1启动系统,在启动过程中,反复按Esc键或者shift键(本人亲测反复按或者长按都可以,没必要纠结),直到出现以下界面: 通过上下键移动,选择U ...
- objective-c数组的七种遍历方法总结
//第一种 [arr enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop){ NSLog(@"%ld,% ...
- linux 文件描述符
文件描述符是什么?和文件句柄有啥区别?文件描述符是linux/unix操作系统中特有的概念.相当于windows系统中的文件句柄.一个意思不同叫法.Linux系统中, 每当进程打开一个文件时,系统就为 ...