leetcode || 104、Maximum Depth of Binary Tree

problem:

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.

Hide Tags

Tree Depth-first
Search

题意:输出二叉树的深度

thinking:

深度优先搜索,记录最大深度

code:

/**
 * Definition for binary tree
 * 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;
        int max_dep=0;
        dfs(0,max_dep,root);
        return max_dep;
    }
protected:
    void dfs(int dep,int &max_dep,TreeNode *node)
    {
        if(node==NULL)
            return;
        dep++;
        max_dep = max(max_dep,dep);
        if(node->left!=NULL)
            dfs(dep,max_dep,node->left);
        if(node->right!=NULL)
            dfs(dep,max_dep,node->right);
    }
};
时间: 2024-07-30 08:43:28

leetcode || 104、Maximum Depth of Binary Tree的相关文章

我也来刷LeetCode——0、Maximum Depth of Binary Tree(开篇)

作为一个非科班出身的程序员,还是很有必要刷刷题的.之前有个大牛说,菜鸟刷题可以从AC率高的刷起,这样可以快速培养信心.将LeetCode的题目按照AC率从高到低排序,第一道题目为 [Maximum Depth of Binary Tree]. 从题目就可以看出,是求二叉树的最大深度.深度的概念,是指从根节点到叶子节点的这条路径上的总节点数. 要求二叉树的最大深度,只需要求出左子树的深度和右子树的深度,取两者最大值再加 1 ,即为二叉树的最大深度. 若该二叉树为空,返回 0 . 然后就是将算法翻译

[LeetCode#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree

The problem 1: 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. My analysis: The recursion solution for this problem is very elegant!It inc

LeetCode OJ - Minimum && Maximum Depth of Binary Tree

这两道题用递归的解法都很简单,只是稍有不同. 下面是AC代码: 1 /** 2 * Given a binary tree, find its minimum depth. 3 * the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 4 * @param root 5 * @return 6 */ 7 public in

leetcode笔记:Maximum Depth of Binary Tree

一. 题目描述 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. 二. 题目分析 这道题和Minimum Depth of Binary Tree一题相似,这个是求最大深度的,就是对二叉树进行递归,然后将子树的最大深度进行返回,最

【leetcode?python】 Maximum Depth of Binary Tree

#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = None class Solution(object):    def maxDepth(self, root):        if

leetcode -day24 Maximum Depth of Binary Tree & Binary Tree Zigzag Level Order Traversal

1.Maximum Depth of Binary Tree 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. class Solution { public: int maxDepth(TreeNode *root) { inM

Leetcode刷题记录[python]——104 Maximum Depth of Binary Tree

一.前言 对于这类抽象问题还有self用法,还要多做多练习,此题是参考其他答案所做. 二.题104 Maximum Depth of Binary Tree 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. # Definitio

[Lintcode]97. Maximum Depth of Binary Tree/[Leetcode]104. Maximum Depth of Binary Tree

97. Maximum Depth of Binary Tree/104. Maximum Depth of Binary Tree 本题难度: Easy Topic: 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 f

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution 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