数据结构----二叉树

1.二叉树的遍历

1.1前序遍历

遍历结果:1 2 3 4 5 6

特点:先打印 根--左树--右树(按这个顺序进入节点就打印)

1.2中序遍历

遍历结果:3 2 1 5 4 6

特点:先打印 左树--根--右树(按这个顺序进入左节点后再出左节点打印)

1.3后序遍历

遍历结果:3 2 5 6 4 1

特点:先打印 左树--右树--根(按这个顺序进入节点后再出节点打印)(左右全走)

代码实现:

复制代码
public class BinaryTree {
      public static class BTNode {
          BTNode left;
          BTNode right;
          int value;

          public BTNode(int value) {
              this.value = value;
          }
      }
      private BTNode root;
    public BTNode createBinaryTree(){
        BTNode node1 = new BTNode(1);
        BTNode node2 = new BTNode(2);
        BTNode node3 = new BTNode(3);
        BTNode node4 = new BTNode(4);
        BTNode node5 = new BTNode(5);
        BTNode node6 = new BTNode(6);
        root = node1;
        node1.left = node2;
        node2.left = node3;
        node1.right = node4;
        node4.left = node5;
        node4.right = node6;
        return root;
    }

    // 前序遍历
    void preOrder(BTNode root){
        if(root == null) {
            return;
        }
        System.out.print(root.value + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    // 中序遍历
    void inOrder(BTNode root){
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.value + " ");
        inOrder(root.right);
    }
    // 后序遍历
    void postOrder(BTNode root){
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.value + " ");
    }



}

2.二叉树的基本操作

2.1获取树中节点个数

第一种方法:

左右树的思想

整棵树节点=左数节点+右数节点个数+1;

然后递归该方法完成。

复制代码
    int size(BTNode root){
        //节点数=左+右+1
        if(root == null) {
            return 0;
        }
        return size(root.left) + size(root.right) + 1;
    }

第二种方法:

计数器,也是最容易想到的方法。

遍历整个树,终止条件为空。

复制代码
public void size(BTNode root){
        //计数器
        if(root == null) {
            return;
        }
        NodeSize++;
        size(root.left);
        size(root.right);
    }

2.2获取叶子节点个数

第一种方法:

同样是最简单的计数器方法。

计数条件是当节点的左右都为空时加一。

复制代码
    public void getLeafNodeCount1(BTNode root){
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafCount++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
    }

注意:该方法没有返回值,结果存储在计数器上。

第二种方法:

利用递归返回值解决问题。

复制代码
    public int getLeafNodeCount2(BTNode root){
        if(root == null) {
            return 0;
        }
        int count = 0;
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

2.3获取第K层节点的个数

使左右同时递归,满足第K层时+1,即可利用递归返回值解决。

复制代码
    int getKLevelNodeCount(BTNode root,int k){
        if(root == null) return -1;
        if(k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.right,k-1)
                + getKLevelNodeCount(root.left,k-1);
    }

2.4获取二叉树的高度

同样利用左右树思想,整个树高度=Max(左树高度+右数高度 )+1

复制代码
    int getHeight(BTNode root){
        if(root == null) return 0;
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return Math.max(rightHeight,leftHeight) + 1;
    }

2.5检测值为value的元素是否存在

左右遍历整棵树即可。

复制代码
    BTNode find(BTNode root, int val){
        if(root == null) return null;
        if(root.value == val) {
            return root;
        }
        BTNode tmp1 = find(root.left,val);
        if(tmp1 != null){
            return tmp1;
        }
        BTNode tmp2 = find(root.right,val);
        if(tmp2 != null){
            return tmp2;
        }
        return null;
    }

2.6层序遍历

层序遍历是逐层打印

思路:

根---左右紧接着相邻打印

使用队列来实现(递归会造成先打印左数再打印右树的情况)

代替递归的小小办法

复制代码
    void levelOrder(BTNode root){
        if(root == null) return;
        Queue<BTNode> queue = new LinkedList<BTNode>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            BTNode cur = queue.poll();
            System.out.print(cur.value + " ");
            if(cur.left != null) {
                queue.offer(cur.left);
            }
            if(cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }
相关推荐
luj_17682 小时前
塔防牌:策略与卡牌的智慧碰撞
服务器·c语言·开发语言·经验分享·算法
wuyk5552 小时前
3.链表:用指针串联的动态数据结构
c语言·开发语言·数据结构·链表
郝学胜-神的一滴2 小时前
干货版《算法导论》17:二叉树核心原理、遍历逻辑与高阶实操全解
数据结构·c++·python·算法·计算机·编程
爱编程的小新☆3 小时前
【LeetCode】从递归到 Flood Fill:5 道题吃透 DFS 的选择、回溯与标记
java·算法·leetcode·深度优先·回溯·flood fill
Water_Sunzhipeng3 小时前
2024牛客暑期多校训练营1
算法
hetao17338373 小时前
2026-08-09~08-14 hetao1733837 的刷题记录
c++·算法
技术小黑3 小时前
RNN算法实战系列06 | LSTM 实现糖尿病探索与预测
rnn·算法·lstm
疯狂打码的少年3 小时前
【数据结构】二叉树的性质(五大性质+计算)
数据结构·笔记·算法
wabs6664 小时前
关于图论【A*算法 | 卡码网127.骑士的攻击的思考】
数据结构·算法·图论·卡码网·广搜的改进版
CIO_Alliance4 小时前
AI认知系列(3)| 数据、算法、算力、场景四要素协同
人工智能·算法·ipaas·系统集成·企业cio联盟·企业级ai化转型