求二叉树的深度和宽度

本文介绍了一种计算二叉树宽度和深度的方法。通过递归方式计算二叉树的高度,采用队列实现来确定二叉树的最大宽度。适用于数据结构与算法的学习者。

题目描述:求二叉树的宽度和深度
给定一个二叉树,获取该二叉树的宽度和深度。

    //二叉树的高度:
    public static int getHeight(BiNode head){
        if(head == null){
            return 0;
        }
        int leftHeight = getHeight(head.left);
        int rightHeight = getHeight(head.right);
        if(leftHeight > rightHeight){
            return leftHeight + 1;
        }
        return rightHeight + 1;
    }

    //二叉树的宽度:
    public static int getWidth(BiNode head){
        if(head == null){
            return 0;
        }
        Queue<BiNode> queue = new ArrayDeque<BiNode>();
        int maxWidth = 1;
        queue.add(head);
        while(true){
            int len = queue.size();
            if(len == 0)
                break;
            while(len > 0){
                BiNode temp = queue.poll();
                len--;
                if(temp.left != null){
                    queue.add(temp.left);
                }
                if(temp.right != null){
                    queue.add(temp.right);
                }
            }
            maxWidth = Math.max(maxWidth, queue.size());
        }
        return maxWidth;
    }
以下是不同实现方式下二叉树最大宽度的主函数示例。 #### 方式一:使用循环深度计算每层宽度 ```c #include <stdio.h> #include <stdlib.h> // 假设的二叉树节点结构体 typedef struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; } BT; // 求树深度 int Depth(BT *root) { if (root == NULL) return 0; int leftDepth = Depth(root->left); int rightDepth = Depth(root->right); return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1; } // 某一层的宽度 int LevelWidth(BT *root, int level) { if (root == NULL) return 0; if (level == 1) return 1; return LevelWidth(root->left, level - 1) + LevelWidth(root->right, level - 1); } // 二叉树的最大宽度 int Width(BT *root) { int width = 0, i; int w[20]; for (i = 0; i < 20; i++) w[i] = 0; if (!root) width = 0; else { for (i = 0; i <= Depth(root); i++) w[i] = LevelWidth(root, i + 1); } i = 0; while (w[i]) { if (w[i] > width) width = w[i]; i++; } return width; } // 创建节点 BT* createNode(int data) { BT* newNode = (BT*)malloc(sizeof(BT)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } int main() { // 创建二叉树的节点 BT* root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); root->right->right = createNode(6); int maxWidth = Width(root); printf("二叉树的最大宽度是: %d\n", maxWidth); return 0; } ``` #### 方式二:使用递归深度数组记录每层节点数 ```c++ #include <iostream> #include <vector> using namespace std; // 假设的二叉树节点结构体 struct Tree { int data; Tree* left; Tree* right; Tree(int val) : data(val), left(nullptr), right(nullptr) {} }; // 二叉树的最大宽度 void Treewidth(Tree* T, int& depth, vector<int>& width) { depth++; if (T == nullptr) { depth--; return; } if (depth >= width.size()) { width.resize(depth + 1, 0); } width[depth]++; Treewidth(T->left, depth, width); Treewidth(T->right, depth, width); depth--; } int main() { // 创建二叉树的节点 Tree* root = new Tree(1); root->left = new Tree(2); root->right = new Tree(3); root->left->left = new Tree(4); root->left->right = new Tree(5); root->right->right = new Tree(6); int depth = -1; vector<int> width; Treewidth(root, depth, width); int maxWidth = 0; for (int w : width) { if (w > maxWidth) maxWidth = w; } cout << "二叉树的最大宽度是: " << maxWidth << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值