binary_tree_max_height

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

binary_tree_max_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 maxDepth(TreeNode* root) {
        if (!root) {
            return 0;
        }
        return max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }

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

你可能感兴趣的文章

binary_tree_inorder

binary_tree_level_order

binary_tree_level_order_ii

0  赞