- 深度优先搜索,递归
maxDepth(TreeNode* root){ if(!root)return 0; return max(maxDepth(root->left),maxDepth(root->right))+1; }
- 广度优先搜索,队列
queue<TreeNode*>q; q.push(root); while(!q.empty()){ int size=q.size(); while(size>0){ TreeNode* node=q.front();q.pop(); if(node->left)q.push(node->left); if(node->right)q.push(node->right); size--; } //一层遍历结束 ans++; }
标签:node,right,leetcode104,搜索,二叉树,push,maxDepth,root,size From: https://www.cnblogs.com/wangkaixin-yy/p/17562748.html