题解 | #二叉树的深度#
二叉树的深度
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) { } };*/ class Solution { public: int TreeDepth(TreeNode* pRoot) { if (pRoot == NULL) { return 0; } int left_deep = TreeDepth(pRoot->left); // 递归获取左子树的深度 int right_deep = TreeDepth(pRoot->right); // 递归获取右子树的深度 return max(left_deep, right_deep) + 1; // 获取最大值+1 } };
2023-剑指-二叉树 文章被收录于专栏
2023-剑指-二叉树