import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class BinaryTree {
static class TreeNode {
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val) {
this.val = val;
}
@Override
public String toString() {
return "TreeNode{" +
"val=" + val +
", left=" + left +
", right=" + right +
'}';
}
}
//以穷举的方式 创建一棵二叉树出来
public TreeNode createTree() {
TreeNode A = new TreeNode('A');
TreeNode B = new TreeNode('B');
TreeNode C = new TreeNode('C');
TreeNode D = new TreeNode('D');
TreeNode E = new TreeNode('E');
TreeNode F = new TreeNode('F');
TreeNode G = new TreeNode('G');
TreeNode H = new TreeNode('H');
A.left = B;
A.right = C;
B.left = D;
B.right = E;
C.left = F;
C.right = G;
return A;
}
// 前序遍历
public void preOrder(TreeNode root) {
if (root == null) {
return;//空树是不需要遍历的
}
System.out.print(root.val + " ");
preOrder(root.left);
preOrder(root.right);
}
// 中序遍历
public void inOrder(TreeNode root) {
if (root == null) {
return;
}
inOrder(root.left);
System.out.print(root.val + " ");
inOrder(root.right);
}
// 后序遍历
public void postOrder(TreeNode root) {
if (root == null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val + " ");
}
//求树的结点数
public int size(TreeNode root) {
if (root == null) {
return 0;
}
int tmp = size(root.left) + size(root.right) + 1;
return tmp;
}
public int leafeNode;
//得到叶子结点的个数
public void getBinaryTreeLeafe1(TreeNode root) {
if (root == null) {
return;
}
if (root.left == null && root.right == null) {
leafeNode++;
}
getBinaryTreeLeafe1(root.left);
getBinaryTreeLeafe1(root.right);
}
public int getBinaryTreeLeafe2(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
return getBinaryTreeLeafe2(root.left) +
getBinaryTreeLeafe2(root.right);
}
//root的第k层有多少个结点
public int getBinaryTreeKNode(TreeNode root, int k) throws Exception {
if (k < 0) {
throw new Exception("k值由误");
}
if (root == null) {
return 0;
}
if (root != null && k == 1) {
return 1;
}
return getBinaryTreeKNode(root.left, k - 1) +
getBinaryTreeKNode(root.right, k - 1);
}
//求树的高度
public int getTreeHeight1(TreeNode root) {
if (root == null) {
return 0;
}
int leftTree = getTreeHeight1(root.left);
int rightTree = getTreeHeight1(root.right);
return Math.max(leftTree, rightTree) + 1;
}
public int getTreeHeight2(TreeNode root) {
if (root == null) {
return 0;
}
int leftTree = getTreeHeight2(root.left);
int rightTree = getTreeHeight2(root.right);
return (leftTree > rightTree ? leftTree : rightTree) + 1;
}
//第三种:不推荐
public int getTreeHeight3(TreeNode root) {
if (root == null) {
return 0;
}
return (getTreeHeight3(root.left) > getTreeHeight3(root.right) ?
getTreeHeight3(root.left) : getTreeHeight3(root.right)) + 1;
}
//找结点
public TreeNode find(TreeNode root, int val) {
if (root == null) {
return null;
}
if (root.val == val) {
return root;
}
TreeNode find1 = find(root.left, val);
if (find1 != null) {
return find1;
}
TreeNode find2 = find(root.right, val);
if (find2 != null) {
return find2;
}
return null;
}
//层序遍历
public void levelOrder(TreeNode root) {
if (root == null) {
System.out.println("树为空");
return;
}
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.offer(root);
while (!nodeQueue.isEmpty()) {
TreeNode tmp = nodeQueue.poll();
System.out.print(tmp.val + " ");
TreeNode left = tmp.left;
TreeNode right = tmp.right;
if (left != null)
nodeQueue.offer(left);
if (right != null)
nodeQueue.offer(right);
}
}
// 判断一棵树是不是完全二叉树
//第一种方法:保留所有的结点(包括空)如果按层次遍历在null后面还有非null结点,那么就不是完全二叉树
public boolean isCompleteTree(TreeNode root) {
if (root == null) {
System.out.println("树为空");
return true;
}
//队列
Queue<TreeNode> nodeQueue = new LinkedList<>();
//顺序表
List<TreeNode> nodeList = new ArrayList<>();
nodeQueue.offer(root);
//全部入队,然后保存起来
int index = 0;
while (!nodeQueue.isEmpty()) {
TreeNode tmp = nodeQueue.poll();
nodeList.add(tmp);
// System.out.println(nodeList.get(index++));
if (tmp != null) {
TreeNode left = tmp.left;
nodeQueue.offer(left);
}
if (tmp != null) {
TreeNode right = tmp.right;
nodeQueue.offer(right);
}
}
//遍历顺序表,看是不是在两棵子树之间有null
int isFlag = -1;
for (int i = 0; i < nodeList.size(); i++) {
//保留遇到第一个null的下标
if (nodeList.get(i) == null) {
isFlag = i;
break;
}
}
for (int i = isFlag; i < nodeList.size(); i++) {
if (nodeList.get(i) != null)
return false;
}
return true;
}
// 判断一棵树是不是完全二叉树
//第二种方法:通过不饱和结点去判断
/*
不饱和结点包含了:(1)只有左子树;(2)只有右子树;(3)没有孩子结点
*/
public boolean isCompleteTree1(TreeNode root) {
if (root == null) {
System.out.print("树为空:");
return true;
}
//队列
Queue<TreeNode> nodeQueue = new LinkedList<>();
//顺序表
nodeQueue.offer(root);
boolean isLeafOrLeft = false;
while (!nodeQueue.isEmpty()) {
TreeNode poll = nodeQueue.poll();
//得到第一个不饱和结点之后
if (isLeafOrLeft) {
//从第一个不饱和结点之后,所有结点都不能有孩子
if (poll.left != null || poll.right != null) {
System.out.println("该二叉树不是完全二叉树");
return false;
}
}//没有找到不饱和结点就继续按照层次遍历寻找
//poll结点的左右孩子都存在
if (poll.left != null && poll.right != null) {
nodeQueue.offer(poll.left);
nodeQueue.offer(poll.right);
} else if (poll.left != null) {
//只有左孩子:找到不饱和结点
nodeQueue.offer(poll.left);
isLeafOrLeft = true;
} else if (poll.right != null) {
//只有右孩子:找到不饱和结点,该树不是完全二叉树
nodeQueue.offer(poll.right);
isLeafOrLeft = true;
// System.out.println("该树不是完全二叉树");
// return false;
} else {
//是叶子结点:找到不饱和结点
isLeafOrLeft = true;
}
}
System.out.println("该树是完全二叉树");
return true;
}
}
02-07
974
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-03
436
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-14
175
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-04
868
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)