牛客题霸-二叉树最大深度题解
bfs求二叉树最大深度
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
typedef struct{
TreeNode* val;
int h;//记录当前结点在第几层//
}node;
int maxDepth(TreeNode* root) {
// write code here
if(root==NULL)return 0;
queue<node>q;
int h;
q.push({root,1});
while(!q.empty()){