一天一道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. dva-quickstart 与 create-react-app 比较(一)

    最近在学习 React ,  现对 dva-quickstart   与  create-react-app 比较 1. 安装, 两个都需要安装工具包:npm install -g create-re ...

  2. 如何避免 async/await 地狱

    简评:async/await 写着很爽,不过要注意这些问题. async/await 让我们摆脱了回调地狱,但是这又引入了 async/await 地狱的问题. 什么是 async/await 地狱 ...

  3. combobox数据绑定

    jquery easyui datagrid 可编辑行 combobox数据绑定问题 将带有参数的url地址赋值给变量,然后将变量赋值给url <script type="text/j ...

  4. vscode 常见插件及配置 备忘

    配置 // 以下解决格式化js自动添加分号 "prettier.singleQuote": true, "prettier.semi": false, // 以 ...

  5. lvs+keepalive实现双主模式(采用DR),同时实现TCP和UDP检测实现非web端的负载均衡,同时实现跨网段的通讯

    因为公司领导需要,需要把lvs备机也使用上,故! 使用双主,相互是主的同时也相互是备机.本人用nat测试发现RS无法实现负载均衡,故采用DR模式来实现非web端的负载均衡 lvs1: DIP 10.6 ...

  6. ubuntu下安装 python 常用软件

    1.用于科学计算的常用包: sudo apt-get install python-numpy python-scipy python-matplotlib ipython ipython-noteb ...

  7. 函数&语法

    定义一个函数 加上一些算法,由自己定义的函数,以下是简单的规则: 函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). 任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参 ...

  8. 深入理解null的原理

    --null的原理 --oracle一直将null和空字符串''<长度为0>同等对待<如'' is null是true,''=null为false,如果声明a varchar2:=' ...

  9. java 里面保留字volatile及其与synchronized的区别

           锁提供了两种主要特性:互斥(mutual exclusion) 和可见性(visibility).互斥即一次只允许一个线程持有某个特定的锁,因此可使用该特性实现对共享数据的协调访问协议, ...

  10. Android ocr识别文字介绍(文字识别)

    最近在做身份证号码识别,在网上搜索的一番后发现目前开源的OCR中tesseract-ocr算是比较强大的了,它由HP于1985年到1995年间开发,后来由google直接负责,经过谷歌进一步开发后,目 ...