LeetCode 104. Maximum Depth of Binary Tree

这篇博客讨论了LeetCode上的问题104,即找到二叉树的最大深度。通过递归和宽度优先遍历(BFS)两种方法进行了解决,其中递归解法简洁,只需一行代码。示例输入为[1,2,3,4,2,4,3],输出为3。文章提供了详细的思路分析和代码实现。" 120463369,10866368,Markdown编辑利器:Typora完全指南,"['前端', '编辑器', 'Markdown', 'Typora']
摘要由CSDN通过智能技术生成

LeetCode 104. Maximum Depth of Binary Tree

Description:

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example:

Input: [1,2,3,4,2,4,3]

   1
 2   2
3 4 4 3

Output: 3


分析:

首先判断特殊情况,根节点为NULL,即返回0,这也是递归终结的条件;然后分别递归左子树和右子树;注意树的深度需要加上1。

一开始我的代码写得比较冗余,注释里我已标注,后来看了LeetCode上面的Discuss说可以一行搞定,我就发现我可以删掉大部分代码;

LeetCode上面还有使用宽度优先遍历BFS算法解决这道题的,主要利用队列的入队、出队操作,我也尝试自己编写了,代码顺便给出来,在文章末尾。

代码如下:

#include <iostream>
using namespace std;
/**
 * 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 maxDepth(TreeNode* root) {
        if (root == NULL) {
            return 0;
        }
        // 可以简化
        // else if (root->left == NULL && root->right == NULL) {
        //  return 1;
        // }
        // else if (root->left != NULL && root->right == NULL) {
        //  return maxDepth(root->left) + 1;
        // }
        // else if (root->left == NULL && root->right != NULL) {
        //  return maxDepth(root->right) + 1;
        // }
        else {
            int leftDepth = maxDepth(root->left) + 1;
            int rightDepth = maxDepth(root->right) + 1;
            return leftDepth > rightDepth ? leftDepth : rightDepth;
        }
    }
};
// 构造二叉树
int TreeNodeCreate(TreeNode* &tree) {
    int val;
    cin >> val;
    if (val < 0) // 小于0表示空节点
        tree = NULL;
    else {
        tree = new TreeNode(val); // 创建根节点
        tree->val = val;
        TreeNodeCreate(tree->left); // 创建左子树
        TreeNodeCreate(tree->right);// 创建右子树
    }
    return 0;
}
int main() {
    Solution s;
    TreeNode* tree;
    TreeNodeCreate(tree);
    cout << s.maxDepth(tree) << endl;
    return 0;
}
/*
input:
1
2
3
-1 -1
4
-1 -1
2
4
-1 -1
3
-1 -1

output:
3
 */

One line:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        return root == NULL ? 0 : max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }
};

宽度优先遍历BFS:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == NULL) return 0;
        int res = 0;
        queue<TreeNode *> q;
        q.push(root);
        while (!q.empty()) {
            res++;
            int n = q.size();
            for (int i = 0; i < n; i++) {
                TreeNode *p = q.front();
                q.pop();
                if (p->left != NULL) q.push(p->left);
                if (p->right != NULL) q.push(p->right);
            }
        }
        return res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值