// 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)_二叉树的更多相关文章

  1. [javaSE] 数据结构(二叉树-遍历与查找)

    前序遍历:中,左,右 中序遍历:左,中,右 后序遍历:左,右,中 二叉树查找 从根节点进行比较,目标比根节点小,指针移动到左边 从根节点进行比较,目标比根节点大,指针移动到右边 /** * 前序遍历 ...

  2. javase(5)_面向对象

    一.概述 1.面向对象是一种思想,让我们由执行者变成指挥者,执行者是面向过程,指挥者是面向对象.例如人开冰箱门,开冰箱门这个动作应该属于门而不是人,冰箱自己最清楚门应该怎么开,人只是调用了冰箱的这个动 ...

  3. javase(1)_基础语法

    一.java概述 1.Java语言特点:纯面向对象(一切皆对象),平台无关(JVM屏蔽底层运行平台的差异),不同的平台有不同的JVM,JVM将程序翻译成当前操作系统能执行的程序,一次编译到处运行),健 ...

  4. c_数据结构_二叉树的遍历实现

    #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 #define OVERFLOW -2 #d ...

  5. c语言_二叉树的建立以及3种递归

    二叉树c语言的实现 二叉树的建立 二叉树的数据结构 typedef struct node{    int data;    struct node* left;    struct node* ri ...

  6. javase(13)_网络编程

    一.概述 1.网络编程的核心是IP.端口(表示应用程序).协议三大元素 2.网络编程的本质是进程间通信 3.网络编程的2个主要问题:1是定位主机,2是数据传输 二.网络通信的概念 1.网络通信协议 计 ...

  7. javase(12)_集合框架_Queue

    一.Queue Queye接口体系图 体系分析: Deque实现类:ArrayDeque, LinkedList(数组和链表实现双向队列) BlockingDeque实现类:LinkedBlockin ...

  8. javase(10)_多线程基础

    一.排队等待 1.下面的这个简单的 Java 程序完成四项不相关的任务.这样的程序有单个控制线程,控制在这四个任务之间线性地移动.此外,因为所需的资源 ― 打印机.磁盘.数据库和显示屏 -- 由于硬件 ...

  9. javase(8)_集合框架_List、Set、Map

    一.集合体系(不包括Queue体系) 二.ArrayList ArrayList的属性 private transient Object[] elementData; //存储元素 private i ...

随机推荐

  1. Unity json

    MiniJSON.cs using UnityEngine; using System; using System.Collections; using System.Collections.Gene ...

  2. Python的一些技巧

    a = [32, 37, 28, 30, 37, 25, 27, 24, 35, 55, 23, 31, 55, 21, 40, 18, 50, 35, 41, 49, 37, 19, 40, 41, ...

  3. 跳转到另一个APP

    看看这个代码: http://code4app.com/codesample/4fcc512d6803fae60b000002 inApp跳转,不过需要Nimbus类库. 要跳转到另一个APP,需要另 ...

  4. GitHub使用方法(初级)

    [初识Github] Git 是一个分布式的版本控制系统,最初由Linus Torvalds编写,用作Linux内核代码的管理.在推出后,Git在其它项目中也取得了很大成功,尤其是在Ruby社区中.目 ...

  5. webpack结合vue使用(五)

    webpack结合vue使用步骤如下: 安装 vue 的包 : cnpm i vue -S 由于在 webpack 中,锐减使用 .vue 这个组件模板文件定义组件,所以需要安装能解析这种文件的第三方 ...

  6. Java 执行linux命令(转)

    转自 http://blog.csdn.net/a19881029/article/details/8063758 java程序中要执行linux命令主要依赖2个类:Process和Runtime 首 ...

  7. RN初始化项目报错

    解决方法:全局删除yarn

  8. 解决“每次启动Access2010时都要求配置VS2008”的办法

    我每次启动Access2010时都会要求配置VS2008,这大概是VS2008与office(Access2010)有冲突引起的.这里提供一种解决办法. 依次选择Access2010的文件-选项中-加 ...

  9. iOS开发:创建推送开发证书和生产证书,以及往极光推送官网上传证书的步骤方法

    在极光官网上面上传应用的极光推送证书的实质其实就是上传导出的p12文件,在极光推送应用管理里面,需要上传两个p12文件,一个是生产证书,一个是开发证书 ,缺一不可,具体如下所示: 在开发者账号里面创建 ...

  10. 081 Search in Rotated Sorted Array II 搜索旋转排序数组 ||

    这是 “搜索旋转排序数组”问题的跟进:如果数组元素允许重复,怎么办?这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?假设按照升序排序的数组在预先未知的某个关键点上旋转.(例如, 0 1 2 4 ...