import java.util.LinkedList;
import java.util.Queue;
public class BinaryTree {
public static class TreeNode{
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val){
this.val = val;//初始化
}
}
public static TreeNode root;
//下面为穷举法创建一个二叉树,也可以用先序遍历方法创建二叉树
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;
E.right = H;
root = A;
return root;
}
public static int nodeSize;
/**
* 树种节点的个数:遍历思路
* @param root
*/
void size(TreeNode root){
if(root == null){
return;
}
nodeSize++;//不为空就有节点 就++
size(root.left);
size(root.right);
}
/**
* 树中节点的个数:子问题思路
* 遍历左右子树 最后+1(根)
* @param root
* @return
*/
public int size2(TreeNode root){
if(root == null){
return 0;
}
return size2(root.left) + size2(root.right) + 1;//+1是根节点
}
/**
* 获取叶子节点个数:遍历思路
*
* @return
*/
void getLeafNodeCount1(TreeNode root) {
if(root == null){
return;
}
if(root.left == null && root.right == null){
nodeSize++;//说明是叶子节点
}
getLeafNodeCount1(root.left);
getLeafNodeCount1(root.right);
}
/**
* 获取叶子节点的个数:子问题
* @param root
* @return
*/
int getLeafNodeCount2(TreeNode root) {
if(root == null){
return 0;
}
if(root.left == null && root.right == null){
return 1;
}
return getLeafNodeCount2(root.left)
+getLeafNodeCount2(root.right);
}
/**
* 获取第K层节点的个数
* @param root 根节点
* @param k 层数
* @return
*/
int getKLevelNodeCount(TreeNode root, int k) {
if(root == null){
return 0;
}
if(k == 1){//k=3 从上到下k-- k==1 说明到了第三层 返回1
return 1;
}
return getKLevelNodeCount(root.left,k-1) +
getKLevelNodeCount(root.right,k-1);
}
/**
* 获取二叉树的高度
* 时间复杂度:O(N)
*return:左右树最大值+1(加的是根节点的那一层)
*/
int getHeight(TreeNode root) {
if(root == null){
return 0;
}
//不为空先求左树高度,再求右树高度
int leftH = getHeight(root.left);//保存值
int rightH = getHeight(root.right);//保存值
//
return leftH > rightH ? leftH+1 : rightH+1;
}
/**
* 检测值为value的元素是否存在
* 用变量接收返回值,变量只有三种情况:①为null ②不为null(不会接收它) ③不为null且值为val
*return:TreeNode
*/
TreeNode find(TreeNode root, char val) {
if (root == null) {
return null;
}
if (root.val == val) {
return root;
}
TreeNode Val1 = find(root.left, val);//返回的root就是要找的val值,传给Val1
if(Val1 != null) {
return Val1;
}
TreeNode Val2 = find(root.right,val);
if(Val2 != null) {
return Val2;
}
return null;//没找到返回空
}
/**
* 层序遍历
* 队列实现遍历
* 思路:根节点入队,队中不为空的时候出节点,打印此节点,将出队节点的左子树和右子树入队(循环)
*/
public void levelOrder(TreeNode root) {
if(root == null){
return;
}
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while(!q.isEmpty()){
TreeNode cur = q.poll();
System.out.print(cur.val);
if(cur.left != null){
q.offer(cur.left);
}
if(cur.right != null) {
q.offer(cur.right);
}
}
}
/** 判断一棵树是不是完全二叉树
* 思路:用队列实现层序遍历,入队:节点为非空,但是左右子树为空(也入队) 因为队列中有空节点时,isEmpty为false
* 节点为空后,break;判断在队中有val值的节点是否还为出队,如果有返回false
* @param root
* @return
*/
public boolean isCompleteTree(TreeNode root) {
if(root == null){
return true;
}
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while(!q.isEmpty()){
TreeNode cur = q.poll();
if(cur != null){//把为null的节点也入队
q.offer(cur.left);
q.offer(cur.right);
}else{
break;
}
}
//如果队列中都为null 则true 如果还有带val值的节点则false
while(!q.isEmpty()){
TreeNode cur = q.poll();
if(cur != null) {
return false;
}
}
return true;
}
}
实现二叉树的基本操作(思路+代码)
最新推荐文章于 2023-05-25 21:30:52 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)