【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方法。

C++:

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int maxDepth(TreeNode *root) {
13         if(root==NULL) return 0;
14         if(root&&root->left==NULL&&root->right==NULL) return 1;
15         int i=1;
16         int j=1;
17         i=maxDepth(root->left);
18         j=maxDepth(root->right);
19
20         if(i>j)
21             return i+1;
22         else
23             return j+1;
24     }
25 };

python:

1 class Solution:
2     # @param root, a tree node
3     # @return an integer
4     def maxDepth(self, root):
5         if root is None:
6             return 0
7         else:
8             return max(self.maxDepth(root.left),self.maxDepth(root.right))+1

递归的思想很重要。

时间: 2024-08-05 11:17:10

【leetcode】Maximum Depth of Binary Tree的相关文章

【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

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

【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. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

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 树 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][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

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

[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

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: 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. 解答: 树的深度优先遍历,求得树高度即可.然后需要用到递归的思想,假设子树的高