Minimum Depth of Binary Tree

本文探讨了如何寻找二叉树的最小深度,并提供了两种不同的算法实现:一种使用递归的先序遍历,另一种采用非递归的层序遍历方式。通过对这两种方法的深入分析,帮助读者理解其原理及应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given abinary tree, find its minimum depth.

 

The minimumdepth is the number of nodes along the shortest path from the root node down tothe nearest leaf node.

 

想来也简单。以先序遍历算法为例,设置一个最小深度变量,在每个叶子节点判断当前深度是否小于最小深度,是则更新最小深度变量。

或者用深度优先遍历,当遍历到叶子节点时,当前深度即为最小深度。

 

解法一

先序遍历的递归算法。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void recurTree(TreeNode* root,int &depth) {
        if(root->left){
            root->left->val=root->val+1;
            recurTree(root->left,depth);
        }
        if(root->right){
            root->right->val=root->val+1;
            recurTree(root->right,depth);
        }
        if(!root->left&&!root->right){
            if(root->val<depth){
                depth=root->val;
            }
        }
        
    }
    int minDepth(TreeNode* root) {
        if(!root)
            return 0;
        root->val=1;
        int depth=2000;
        recurTree(root,depth);
        return depth;
    }
};


解法二

数组实现的层序遍历的非递归算法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root)
            return 0;
        TreeNode* list[2000];
        int front=-1,rear=-1;
        int level=0,last=0;
        list[++rear]=root;
        while(front<rear){
            TreeNode *temp;
            temp=list[++front];
            if(temp->left)
                list[++rear]=temp->left;
            if(temp->right)
                list[++rear]=temp->right;
            if((!temp->left)&&(!temp->right)){
                return level+1;
            }
            if(front==last){
                last=rear;
                level++;
            }
        }
        return level;
    }
};


看了讨论之后,觉得自己的代码不够简洁高效。又重新写了两个改进后的代码


更简洁的dfs解法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root)
            return 0;
        if(root->left&&root->right)
            return min(minDepth(root->left),minDepth(root->right))+1;
        return max(minDepth(root->left),minDepth(root->right))+1;
    }
};

用queue实现的层序遍历非递归算法

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {
        if(!root)
            return 0;
        queue<TreeNode*> q;
        int level=0;
        q.push(root);
        while(!q.empty()){
            int vol=q.size();
            level++;
            for(int i=0;i<vol;i++){
                TreeNode *node;
                node=q.front();
                if(!node->left&&!node->right)
                    return level;
                if(node->left)
                    q.push(node->left);
                if(node->right)
                    q.push(node->right);
                q.pop();
            }
        }
        return level;
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值