二叉树常用方法(一)
树节点
public class Node {
public int data;
public Node leftNode;
public Node rightNode;
public Node(){
this.data = -1;
}
public Node(int data){
this.data = data;
this.leftNode = null;
this.rightNode = null;
}
}
二叉树方法
二叉树
public class Tree {
private Node root;
public Tree(){
root = null;
}
}
前序数组创建二叉树
public int creatTreeByPre(Node node, int[] treeNodes, int n){
if (node == null) {
root = new Node();
node = root;
}
node.data = treeNodes[n];
int left =0,right=0;
if(treeNodes[n+1] != -1){
node.leftNode = new Node();
left = creatTreeByPre(node.leftNode,treeNodes,n+1);
}else {
left = n+1+1;
}
if (treeNodes[left]!=-1){
node.rightNode = new Node();
right = creatTreeByPre(node.rightNode,treeNodes,left);
}else {
right = left+1;
}
return right;
}
前序遍历
public void printNodeByPre(Node root){
if (root==null) return;
System.out.print(root.data+"\t");
printNodeByPre(root.leftNode);
printNodeByPre(root.rightNode);
}
求树最大深度
public int maxDeath(Node node){
if (node == null) return 0;
int left = maxDeath(node.leftNode);
int right = maxDeath(node.rightNode);
return Math.max(left,right)+1;
}
求二叉树节点个数
public int getNodenum(Node root){
if (root == null) return 0;
int left = getNodenum(root.leftNode);
int right = getNodenum(root.rightNode);
return left+right+1;
}
求树最小深度
public int minDeath(Node root){
if (root == null) return 0;
int left = minDeath(root.leftNode);
int right = minDeath(root.rightNode);
return Math.min(left,right)+1;
}
求左右树节点个数(不包含根节点)
public int getNumOfChildNode(Node root){
if (root == null) return 0;
int left = getNumOfChildNode(root.leftNode);
int right = getNumOfChildNode(root.rightNode);
return left+right;
}
判断是否为平衡二叉树
public boolean isbalancedTree(Node root){
return getAbsDeath(root,1)!=-1;
}
public int getAbsDeath(Node root,int n){
if (root == null) return 0;
int left = getAbsDeath(root.leftNode,n);
int right = getAbsDeath(root.rightNode,n);
if (left ==-1 || right==-1 || Math.abs(left-right)>n) return -1;
return Math.max(left,right)+1;
}
判断是否为完美树
public boolean isCompleteTree(Node root){
if (root == null) return false;
Queue<Node> queue = new LinkedList<>();
boolean hasChild = true;
while (!queue.isEmpty()){
if (hasChild){
if (root.leftNode == null &&root.rightNode == null)
hasChild = false;
if (root.leftNode != null && root.rightNode == null){
hasChild = false;
queue.add(root.leftNode);
}
if (root.leftNode == null && root.rightNode != null){
return false;
}
if (root.leftNode != null && root.rightNode !=null){
queue.add(root.leftNode);
queue.add(root.rightNode);
}
}else {
if (root.leftNode != null || root.rightNode != null)
return false;
}
}
return true;
}
判断两棵树是否相等
public boolean isSameTree(Node root,Node otherroot){
if (otherroot == null && root == null) return true;
else if (otherroot != null && root == null) return false;
else if (otherroot == null && root != null) return false;
boolean mainresult = (otherroot.data == root.data);
boolean leftresult = isSameTree(root.leftNode,otherroot.leftNode);
boolean rightresult = isSameTree(root.rightNode,otherroot.rightNode);
return mainresult && leftresult && rightresult;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)