【一天一道LeetCode】#104. Maximum Depth of Binary Tree
来源:互联网 发布:尚学堂 js 编辑:程序博客网 时间:2024/06/05 01:42
一天一道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);//遍历右子树 }};
0 2
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
- [LeetCode]104.Maximum Depth of Binary Tree
- 【LeetCode】104.Maximum Depth of Binary Tree
- [Leetcode] 104. Maximum Depth of Binary Tree
- LeetCode --- 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- leetCode 104. Maximum Depth of Binary Tree
- 104. Maximum Depth of Binary Tree LeetCode
- 104. Maximum Depth of Binary Tree LeetCode
- LeetCode 104. Maximum Depth of Binary Tree
- leetcode:104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- [LeetCode]104. Maximum Depth of Binary Tree
- leetcode 104. Maximum Depth of Binary Tree
- LeetCode 104. Maximum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree
- LeetCode#104. Maximum Depth of Binary Tree
- Leetcode 104. Maximum Depth of Binary Tree
- java注解讲解与实用示例
- oracle的表分区
- HTML5 语义元素
- mysql之delete删除记录后数据库大小不变
- 【C#】32. 使用EF(EntityFramework)连接Mysql,MVC开发
- 【一天一道LeetCode】#104. Maximum Depth of Binary Tree
- java基础知识之常量与变量
- 【leetcode】234. Palindrome Linked List
- Windows下的SVN安装及使用
- Linux开放端口
- mac中github使用.ppk密钥通过ssh协议clone代码
- Leetcode 116. Populating Next Right Pointers in Each Node
- c++中抽象类与接口的区别
- 数据系统架构——Lambda architecture(Lambda架构)