binary_tree_min_height

  • 2022-12-14
  • 浏览 (505)

binary_tree_min_height.cpp 源码

// 二叉树的最小深度

#include "vector"
#include "queue"
#include "algorithm"

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() {}
    TreeNode(int val) : val(val) {}
    TreeNode(int val, TreeNode *left, TreeNode *right) : val(val), left(left), right(right) {}
};

class Solution {
public:

    // 深度优先
    int minDepth(TreeNode* root) {
        if (!root) {
            return 0;
        }
        int left = minDepth(root->left);
        int right = minDepth(root->right);
        return (left == 0 || right == 0) ? left + right + 1 : min(left, right) + 1;
    }

    // 广度优先
    int minDepth1(TreeNode* root) {
        if (!root) {
            return 0;
        }
        
        queue<TreeNode*> que;
        que.push(root);
        int res = 0;
        while(!que.empty()) {
            int size = que.size();
            res++;
            while(size-- > 0) {
                TreeNode* node = que.front();
                que.pop();
                if (!node->left && !node->right) {
                    return res;
                }
                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
        }
        return res;
    }
};

你可能感兴趣的文章

binary_tree_inorder

binary_tree_level_order

binary_tree_level_order_ii

0  赞