javase(3)_二叉树
// 1.求二叉树中的节点个数
// 2.求二叉树的深度
// 3.前序遍历,中序遍历,后序遍历
// 4.分层遍历二叉树(按层次从上往下,从左往右)
// 5.将二叉查找树变为有序的双向链表
// 6.求二叉树第K层的节点个数
// 7.求二叉树中叶子节点的个数
// 8.判断两棵二叉树是否结构相同
// 9.判断二叉树是不是平衡二叉树
// 10.判断二叉树是不是完全二叉树
节点
class Node{
Node leftChild;
Node rightChild;
Object value;
public Node(Node leftChild, Node rightChild, Object value) {
this.leftChild = leftChild;
this.rightChild = rightChild;
this.value = value;
}
}
实现
//求二叉树节点个数
public static int getNodeNum(Node root){
if(root==null)
return 0;
return getNodeNum(root.leftChild)+getNodeNum(root.rightChild)+1;
}
//求二叉树的深度
public static int getDepth(Node root){
if(root==null)
return 0;
int leftDepth = getDepth(root.leftChild);
int rightDepth = getDepth(root.rightChild);
return leftDepth>=rightDepth?leftDepth+1:rightDepth+1;
}
//前序遍历,中续遍历,后续遍历
public static void preOrderTraversal(Node root){
if(root==null)
return;
System.out.print(root.value);
preOrderTraversal(root.leftChild);
preOrderTraversal(root.rightChild);
}
public static void inOrderTraversal(Node root){
if(root==null)
return;
inOrderTraversal(root.leftChild);
System.out.print(root.value);
inOrderTraversal(root.rightChild);
}
public static void postOrderTraversal(Node root){
if(root==null)
return;
postOrderTraversal(root.leftChild);
postOrderTraversal(root.rightChild);
System.out.print(root.value);
}
//分层遍历二叉树(按层次从上往下,从左往右)
public static void levelTraversal(Node root){
if(root==null)
return;
LinkedList<Node> ll = new LinkedList<Node>();
ll.offer(root);
while(!ll.isEmpty()){
root=ll.poll();
System.out.println(root.value);
if(root.leftChild!=null)
ll.offer(root.leftChild);
if(root.rightChild!=null)
ll.offer(root.rightChild);
}
}
//二叉查找树变为有序的双向链表
static LinkedList<Node> ll=new LinkedList<Node>();
public static LinkedList<Node> binarySearchTreetoDoubleEndList(Node node){
if(node==null)
return null;
binarySearchTreetoDoubleEndList(node.leftChild);
ll.offer(node);
binarySearchTreetoDoubleEndList(node.rightChild);
return ll;
}
//求二叉树第K层的节点个数
public static int getNodeNumkthLevel(Node root,int k){
if(root==null)
return 0;
if(root.leftChild==null&&root.rightChild==null) //if(k==1)
return 1;
int leftNum=getNodeNumkthLevel(root.leftChild,k-1);
int rightNum=getNodeNumkthLevel(root.rightChild,k-1);
return leftNum+rightNum;
}
//求二叉树中叶子节点的个数
public static int getleafNodeNum(Node root){
if(root==null)
return 0;
if(root.leftChild==null&&root.rightChild==null)
return 1;
int leftNum=getleafNodeNum(root.leftChild);
int rightNum=getleafNodeNum(root.rightChild);
return leftNum+rightNum;
}
//判断两棵二叉树是否结构相同
public static boolean isSameTree(Node n1,Node n2){
if(n1==null&&n2==null)
return true;
if(n1==null||n2==null)
return false;
boolean l = isSameTree(n1.leftChild,n2.leftChild);
boolean r = isSameTree(n1.rightChild,n2.rightChild);
return l&&r;
}
//判断二叉树是不是平衡二叉树
public static boolean isBalanceTree(Node root){
if(Math.abs(getDepth(root.leftChild)-getDepth(root.rightChild))>1)
return false;
if(Math.abs(getDepth(root.leftChild)-getDepth(root.rightChild))<1)
return true;
boolean l = isBalanceTree(root.leftChild);
boolean r = isBalanceTree(root.rightChild);
return l&&r;
}
//判断二叉树是不是完全二叉树
public static boolean isCompleteTree(Node root){
if(root.leftChild==null&&root.rightChild==null)
return true;
if(root.leftChild==null||root.rightChild==null)
return false;
boolean l = isCompleteTree(root.leftChild);
boolean r = isCompleteTree(root.rightChild);
return l&&r;
}
javase(3)_二叉树的更多相关文章
- [javaSE] 数据结构(二叉树-遍历与查找)
前序遍历:中,左,右 中序遍历:左,中,右 后序遍历:左,右,中 二叉树查找 从根节点进行比较,目标比根节点小,指针移动到左边 从根节点进行比较,目标比根节点大,指针移动到右边 /** * 前序遍历 ...
- javase(5)_面向对象
一.概述 1.面向对象是一种思想,让我们由执行者变成指挥者,执行者是面向过程,指挥者是面向对象.例如人开冰箱门,开冰箱门这个动作应该属于门而不是人,冰箱自己最清楚门应该怎么开,人只是调用了冰箱的这个动 ...
- javase(1)_基础语法
一.java概述 1.Java语言特点:纯面向对象(一切皆对象),平台无关(JVM屏蔽底层运行平台的差异),不同的平台有不同的JVM,JVM将程序翻译成当前操作系统能执行的程序,一次编译到处运行),健 ...
- c_数据结构_二叉树的遍历实现
#include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #d ...
- c语言_二叉树的建立以及3种递归
二叉树c语言的实现 二叉树的建立 二叉树的数据结构 typedef struct node{ int data; struct node* left; struct node* ri ...
- javase(13)_网络编程
一.概述 1.网络编程的核心是IP.端口(表示应用程序).协议三大元素 2.网络编程的本质是进程间通信 3.网络编程的2个主要问题:1是定位主机,2是数据传输 二.网络通信的概念 1.网络通信协议 计 ...
- javase(12)_集合框架_Queue
一.Queue Queye接口体系图 体系分析: Deque实现类:ArrayDeque, LinkedList(数组和链表实现双向队列) BlockingDeque实现类:LinkedBlockin ...
- javase(10)_多线程基础
一.排队等待 1.下面的这个简单的 Java 程序完成四项不相关的任务.这样的程序有单个控制线程,控制在这四个任务之间线性地移动.此外,因为所需的资源 ― 打印机.磁盘.数据库和显示屏 -- 由于硬件 ...
- javase(8)_集合框架_List、Set、Map
一.集合体系(不包括Queue体系) 二.ArrayList ArrayList的属性 private transient Object[] elementData; //存储元素 private i ...
随机推荐
- Some JPR highlights (JPR 2019 March)
Journal Name:Journal of Proteome Research Issue:2019 March Shared by: Weining Zhao 1. Acetylome: ...
- (八)SpringBoot使用mybatis-plus+自动代码生成
一.SpringBoot使用mybatis-plus+自动代码生成 使用mybatis-plus可以自动帮我们生成通用的 controller,service,dao,mapper 二.加入依赖 &l ...
- MyBatist庖丁解牛(一)
站在巨人的肩膀上,感谢! https://www.jianshu.com/p/ec40a82cae28?utm_campaign=maleskine&utm_content=note& ...
- C - Brackets
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> ...
- 执行gulp build报错
问题与分析 在执行gulp build报错如下: D:\coding\Resume\Resumes>gulp build gulp build[5628]: src\node_contextif ...
- python_argparse
使用python argparser处理命令行参数 #coding:utf-8 # 导入模块 import argparse # 创建ArgumentParser()对象 parser = argpa ...
- go系列(1)- linux下安装go环境
安装GO 打开安装包下载地址,查看linux下go的最新版本 https://golang.google.cn/dl/ 经查看go的最新版本为go1.11.4.linux-amd64.tar.gz 右 ...
- spring+mybits 整合所需jar包的下载路径(亲测有效)
1.spring jar包:http://repo.springsource.org/libs-release-local/org/springframework/spring/5.0.0.RELEA ...
- [題解]luogu_P1144最短路計數
1.無權圖最短路邊權為1 2.如果兩個點恰好不能被更新(d[y]==d[x]+1)那麼就能通過x的所有最短路到達y,所以ans[y]+=ans[x] 3.如果兩個點不能恰好被更新(d[y]>d[ ...
- 洛谷 P3957 跳房子
https://www.luogu.org/problemnew/show/P3957 错误记录:1.没开longlong 2. -inf不够小 #include<cstdio> #inc ...