leetcode:111. 二叉树的最小深度

题目来源

111. 二叉树的最小深度

题目描述

在这里插入图片描述

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};


class Solution {
public:
    int minDepth(TreeNode* root) {

    }
};

题目解析

层次遍历

int minDepth(TreeNode* root) {
    if(root == NULL){
        return 0;
    }

    std::queue<TreeNode *> queue;
    queue.push(root);
    int level = 0;
    while (!queue.empty()){
        level++;
        int size = queue.size();
        for(int i = 0; i < size; i++){
            TreeNode *node = queue.front();
            queue.pop();
            if(node->left == NULL && node->right == NULL){
                return level;
            }
            if(node->left){
                queue.push(node->left);
            }
            if(node->right){
                queue.push(node->right);
            }
        }

    }

    return level;
}

DP

算法:分治/动态规划/DFS
实现方式:自底向上/递归

int minDepth(TreeNode* root) {
	 // 基本情况 dp[null] = 0 ,dp[叶子] = 1
    if(root == NULL){
        return 0;
    }
    
    if(root->left == NULL || root->right == NULL){
        return 1;
    }
    
    if(root->right == NULL){
        return minDepth(root->left) + 1;
    }
    
    if(root->left == NULL){
        return minDepth(root->right) + 1;
    }
    
      // 状态转移方程
        //              / dp[右孩子] + 1 ,左孩子为null
        // dp[非叶子] =    dp[左孩子] + 1 ,右孩子为null
        //              \ min(dp[左孩子], dp[右孩子]) + 1 ,左右孩子都不为null
    return std::min(minDepth(root->left), minDepth(root->right)) + 1;
}

回溯

class Solution {
public:
    int minDepth(TreeNode* root) {
        return helper(root, 1);
    }


    int helper(TreeNode *root, int depth){
        if(root == NULL){
            return depth - 1;
        }

        if(root->left == NULL && root->right == NULL){
            return depth ;
        }

        if(root->left == NULL){
            return helper(root->right, depth + 1);
        }

        if(root->right == NULL){
            return helper(root->left, depth + 1);
        }

        return std::min(helper(root->right, depth + 1), helper(root->left, depth + 1));
    }
};

深度优先搜索

class Solution {
private:
    int ans = INTMAX_MAX;
public:
    int minDepth(TreeNode* root) {
        helper(root, 0);
        return  ans;
    }


    void helper(TreeNode *root, int depth){
        if(root == NULL){
            ans = depth;
            return ;
        }
        
        depth++;
        
        if(root->left == NULL && root->right == NULL){
            if(depth < ans){
                ans = depth;
            }
            return;
        }
        
        if(root->left == NULL){
            helper(root->left, depth);
        }

        if (root->right != NULL){
            helper(root->right, depth);
        }
    }
};

morris遍历

class Solution {
public:
    int minDepth(TreeNode* head) {
        if(head == nullptr){
            return 0;
        }
        
        TreeNode *curr = head;
        TreeNode *mostRight = nullptr;
        int currLevel = 0;
        int minHeight = INT32_MAX;
        while (curr != nullptr){
            mostRight = curr->left;
            if(mostRight != nullptr){
                int rightBoardSize = 1;
                while (mostRight->right != nullptr && mostRight->right != curr){
                    rightBoardSize++;
                    mostRight = mostRight->right;
                }
                if(mostRight->right == nullptr){ // 第一次到达
                    currLevel++; //	当前节点一定是从父节点下来的
                    mostRight->right = curr;
                    curr = curr->left;
                    continue;
                }else{ // 第二次到达
                    if (mostRight->left == nullptr) {  //此时为叶子节点,因此更新最小深度
                        minHeight = std::min(minHeight, currLevel);
                    }
                    currLevel -= rightBoardSize;  //第二次返回父节点(当前节点一定是从子节点上来的),需要更新当前节点
                    mostRight->right = nullptr;
                }
            }else{
                currLevel++;
            }
            curr = curr->right;
        }
        int finalRight = 1;
        curr = head;
        while (curr->right != nullptr){
            finalRight++;
            curr = curr->right;
        }
        if(curr->left != nullptr && curr->right != nullptr){
            minHeight = std::min(minHeight, currLevel);
        }
        return minHeight;
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值