一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

来源:https://leetcode.com/problems/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.

(二)解题

题目大意:求二叉树的最大深度

解题思路:采用深度优先搜索,很容易求出最大深度

/**
 * 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 max;//用来保存最大深度值
    int maxDepth(TreeNode* root) {
        max = 0;
        dfsTree(root,0);//深度优先搜索递归
        return max;
    }
    void dfsTree(TreeNode* root , int dep)
    {
        if(dep>max) max=dep;//记录最大深度值
        if(root==NULL) return;
        dfsTree(root->left,dep+1);//遍历左子树
        dfsTree(root->right,dep+1);//遍历右子树
    }
};

【一天一道LeetCode】#104. Maximum Depth of Binary Tree的更多相关文章

  1. LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告

    104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...

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

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

  3. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  4. (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  6. 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 ...

  7. leetcode 104 Maximum Depth of Binary Tree ----- java

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  8. Java [Leetcode 104]Maximum Depth of Binary Tree

    题目描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...

  9. Leetcode 104. Maximum Depth of Binary Tree(二叉树的最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  10. Leetcode 104 Maximum Depth of Binary Tree python

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

随机推荐

  1. Java Socket通信代码片

    package zhang; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOExcept ...

  2. Vue nextTick 机制

    背景 我们先来看一段Vue的执行代码: export default { data () { return { msg: 0 } }, mounted () { this.msg = 1 this.m ...

  3. centos 7.X & centos6.X 防火墙基本命令

    Centos 7 firewall 命令:查看已经开放的端口: firewall-cmd --list-ports 开启端口 firewall-cmd --zone=public --add-port ...

  4. FJUT第三周寒假作业《第九集,离间计》栈

    第九集,离间计 TimeLimit:1000MS  MemoryLimit:128MB 64-bit integer IO format:%I64d   Problem Description 拥有了 ...

  5. JAVA中抽象类的使用

    抽象类是从多个具体类中抽象出来的父类,它具有更高层次的抽象.抽象类体现的就是一种模板模式的设计,抽象父类可以只定义需要使用的某些方法,把不能实现的某些部分抽象成抽象方法,留给其子类去实现.具体来说,抽 ...

  6. JavaC命令不能被执行尴尬问题解决

    安装和配置环境变量都按着流程在,但在最后的检验时,发现Java   Java -version 都能运行,唯独Javac 报"不能识别命令"错误信息,下面列出我遇到一个尴尬问题 在 ...

  7. node安装教程

    推荐安装教程博客: https://www.cnblogs.com/zhouyu2017/p/6485265.html

  8. ACM Red and Black

    有一个矩形的房间,覆盖着方砖. 每个瓷砖都是红色或黑色. 一个男人站在黑色的瓷砖上,他可以移动到四个相邻的瓷砖之一.  但他不能在红砖上移动,他只能在黑砖上移动. 编写一个程序来计算他可以通过重复上述 ...

  9. 整理的Java List Set Map是否有序,元素是否允许重复

    整理的Java List Set Map是否有序,元素是否允许重复的说明,如下图:

  10. Rails做rspec测试时出现bcrypt错误的解决

    在用rspec做测试的时候,出现了如下一句错误: You don't have bcrypt-ruby installed in your application. Please add it to ...