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 farthest leaf node.

Hide Tags

Tree Depth-first Search

Have you met this question in a real interview?

Yes

No

Discuss

1.采用递归的方法来做

#include<iostream>
#include<vector>
using namespace std;

struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

//用递归的方法来做(有点类似于动态规划的感觉)
int max(int a,int b);
int maxDepth(TreeNode *root)
{
	int left,right;
	if(root==NULL)
		return 0;
	left=maxDepth(root->left);//左子树的深度
	right=maxDepth(root->right);//右子树的深度
	return 1+max(left,right);//选取其中深度大的那个,再加上本身的一,为本身这个结点深度
}
int max(int a,int b)
{
	if(a>=b)
		return a;
	else
		return b;
}
int main(int argc,char** argv)
{

}

  2.采用队列的方式来做

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

struct TreeNode
{
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

//采用队列的方式来做,采用队列的方式一层一层的对二叉树进行遍历,并记录层数
int maxDepth(TreeNode *root)
{
	if(root==NULL)
		return 0;
	int deep=0;//深度
	int row_size=1;//每一层的元素个数(第一层只有一个根节点)
	queue<TreeNode*> temp;//算法所用的队列.

	temp.push(root);
	while(!temp.empty())//直到队列为空才停止
	{
		TreeNode* temp_node;
		while(row_size--)//依次的遍历每一层,将下一层的每一个元素都进队列,并将上一层
		{//的队列都出列。
			temp_node=temp.front();
			if(temp_node->left!=NULL)
				temp.push(temp_node->left);
			if(temp_node->right!=NULL)
				temp.push(temp_node->right);
			temp.pop();
		}
		row_size=temp.size();//记录下这一层的元素有多少个
		deep+=1;//记录深度
	}
	return deep;
}

int main(int argc,char** argv)
{

}

  

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

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. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://

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 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 (求树的深度)

题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回: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 *

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

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 OJ - Minimum &amp;&amp; Maximum Depth of Binary Tree

这两道题用递归的解法都很简单,只是稍有不同. 下面是AC代码: 1 /** 2 * Given a binary tree, find its minimum depth. 3 * the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 4 * @param root 5 * @return 6 */ 7 public in

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