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.

有两种求解的思路,一种采用DFS的思想,一种采用BFS的思想,如下代码所示:

 1 struct TreeNode {
 2     int            val;
 3     TreeNode*    left;
 4     TreeNode*    right;
 5     TreeNode(int x): val(x), left(NULL),right(NULL) {}
 6 };
 7
 8 //采用DFS的思想
 9 int maxDepth(TreeNode *root)
10 {
11     if (NULL == root)
12         return 0;
13     int l = maxDepth(root->left);
14     int r = maxDepth(root->right);
15
16     return l > r ? l + 1:r+1;
17     //以上这两种方式有一种更简便的方法
18     //return 1 + max(maxDepth(root->left), maxDepth(root->right));
19 }
20
21 //采用BFS的方法,引入队列
22 int maxDepth(TreeNode *root)
23 {
24     if (NULL == root)
25         return 0;
26     queue <TreeNode *> que;
27     int nCount = 1;
28     int nDepth = 0;// 记录队列里面每一层上的元素
29
30     que.push(root);
31     while(!que.empty()) {
32         TreeNode *pTemp = que.front();
33         que.pop();
34         nCount --;
35
36         if (pTemp->left)
37             que.push(pTemp->left);
38         if (pTemp->right)
39             que.push(pTemp->right);
40
41         if (nCount == 0) {
42             nDepth ++;
43             nCount = que.size();
44         }
45     }
46     return nDepth;
47 }
时间: 2024-10-22 15:30:31

LeetCode:Maximum Depth of Binary Tree的相关文章

leetcode -day24 Maximum Depth of Binary Tree &amp; 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 树 Maximum Depth of Binary Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Maximum Depth of Binary Tree Total Accepted: 16605 Total Submissions: 38287 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the ro

【LeetCode】 Maximum Depth of Binary Tree

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 getMax(TreeNode *root

学习笔记:LeetCode104:Maximum Depth of Binary Tree

LeetCode104: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. 求二叉树的最大深度,树节点包含当前值及分别指向其左右子节点的引用,如下: 1 /** 2 * De

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

LeetCode:Minimum Depth of Binary Tree

要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意(l<r)? l+1:r+1的区别应用,因为可能存在左右子树为空的情况,此时值就为0,但显然值是不为0的(只有当二叉树为空才为0),所以,在这里注意一下即可! 代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode *left; 4 TreeNode *rig

[LeetCode][Java]Maximum Depth of Binary Tree

https://leetcode.com/problems/maximum-depth-of-binary-tree/ 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 1

[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笔记: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一题相似,这个是求最大深度的,就是对二叉树进行递归,然后将子树的最大深度进行返回,最