/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
//calculate height of binary tree if given the head node
int calculateDepth(TreeNode *node)
{
if(node==NULL)
return 0;
int depth;
int leftdepth=calculateDepth(node->left)+1;
int rightdepth=calculateDepth(node->right)+1;
depth=max(leftdepth,rightdepth);
return depth;
}
};
计算二叉树的深度(递归算法)
最新推荐文章于 2024-09-14 22:25:23 发布