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 ...
随机推荐
- hdu3257【模拟】
题意: 从案例找: 思路: 就是16进制,然后到2进制= =.就是个模拟= =.注意格式: #include <bits/stdc++.h> using namespace std; ty ...
- Gitlab备份,Crontab定时备份
1:Gitlab备份非常简单,只需要一条命令就可以创建完整的备份 gitlab-rake gitlab:backup:create 使用以上命令,就相当于在/var/opt/gitlab/backup ...
- 去掉UItalbeview横线
一.去掉UItalbeview中所有横线 // self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; 二.自定义U ...
- djangoAdmin组件
定制后台页面功能 from django.contrib import admin from app import models # Register your models here. class ...
- flask环境安装
virtualenv venv #创建venv .venv/bin/activate #进入venv venv/bin/pip install flask venv/bin/pip install f ...
- 最短路之Dijkstra(单源)HDU 2544
#include <iostream> using namespace std; ; ][]; ]; int middist; ]; void dijkstra(int n,int m) ...
- C/C++程序员应聘常见面试题深入剖析(2)
摘自:http://blog.csdn.net/zhoudengqing 3.内功题 试题1:分别给出BOOL,int,float,指针变量 与“零值”比较的 if 语句(假设变量名为var) 解答: ...
- java 利用c3p0管理数据库连接池
数据库连接池类,用于获取数据库连接.利用单例模式保证所有的连接都只通过一个连接池管理. package com.mousewheel.dbcon; import java.io.InputStream ...
- 关于Pre-bound JDBC Connection found! HibernateTransactionManager does not 异常小结
昨天帮女朋友配置ssh框架的多数据源, 本以为对此已经很熟悉,配置完其他的错倒是还能接受,调了一下就好了. 唯独 Pre-bound JDBC Connection found! Hibernate ...
- Java 多个if 和多个else if 的区别
int a=1; if(a==1){System.out.println("1");} if(a==2){System.out.println("2");} i ...