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.

题解: 题意比较清楚, 找到从root出发最长的一条路径的长度。 采用DFS即可。

相似的一道题: Minimux Depth of Binary Tree 解法: http://www.cnblogs.com/double-win/p/3737237.html


 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 minDepth(TreeNode *root) {
13 if(root==NULL) return 0;
14 if(root->left==NULL && root->right==NULL) return 1;
15
16 int Leftdepth = minDepth(root->left);
17 int Rightdepth = minDepth(root->right);
18
19 if(Leftdepth==0)
20 return Rightdepth+1;
21 else if(Rightdepth==0)
22 return Leftdepth+1;
23
24 return min(Leftdepth,Rightdepth)+1;
25 }
26 };

LeetCode: Maximum Depth of Binary Tree 题解,布布扣,bubuko.com

时间: 2024-10-25 07:01:22

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

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. 此题和求二叉树的最短路径几乎一模一样. public int maxDepth(TreeNode root) { if (root == null) return 0; int a

leetcode——Maximum Depth of Binary Tree (递归,)

Maximum Depth of Binary Tree Total Accepted: 59837 Total Submissions: 132940My 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 f

[LeetCode] Maximum Depth of Binary Tree dfs,深度搜索

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. Hide Tags Tree Depth-first Search 简单的深度搜索 #include <iostream> using namespace std; /** *

leetcode Maximum Depth of Binary Tree

代码: /** * 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; void deepSearch(TreeNode * root, int dep

leetcode Maximum Depth of Binary Tree python

# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def maxDepth(self, root): """ :type root: TreeNode :rtype: int ""

LeetCode Maximum Depth of Binary Tree (求树的深度)

题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 *

Maximum Depth of Binary Tree leetcode java

题目: 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     public int maxDepth(TreeNode root) {2         if(root==null)3         

【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】 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