题解 | #二叉树的深度#

二叉树的深度

https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b

/*
struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
			val(x), left(NULL), right(NULL) {
	}
};*/
#include <algorithm>
class Solution {
public:
    int TreeDepth(TreeNode* pRoot) {
		if (pRoot == NULL) {
			return 0;
		}
		return max(TreeDepth(pRoot->left), TreeDepth(pRoot->right)) + 1;
    }
};

递归求二叉树深度,其实等于求最大递归深度,递归求左右子树的最大深度加一

全部评论

相关推荐

09-04 21:05
已编辑
西南科技大学 Java
点赞 评论 收藏
分享
牛客339922477号:都不用reverse,直接-1。一行。啥送分题
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务