The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 1/**2* Definition for binary tree3* struct TreeNode {4* int val;5* TreeNode *left;6* TreeNode *right;7* TreeNode(int x) : val(x), left(NULL), right(NULL) {}...
* Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int maxDepth(TreeNode *root) { if(root==NULL) return 0; return max(maxDepth(root-...
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的距离。 【题目链接】 www.lintcode.com/en/problem/maximum-d...
AI代码解释 intmaxDepth(TreeNode*root){if(root==NULL)return0;queue<TreeNode*>btQueue;//使用队列进行层序遍历btQueue.push(root);int count=1;//记录当前层的节点个数int depth=0;//记录当前遍历的深度while(!btQueue.empty()){TreeNode*currentNode=btQueue.front();btQueue.pop();//每次循环出队一...
clone()), Self::max_depth(root.borrow().right.clone()), ) } else { // 如果根结点不存在,则返回 0 0 } } } 题目链接: Maximum Depth of Binary Tree : leetcode.com/problems/m 二叉树的最大深度: leetcode-cn.com/problem LeetCode 日更第 31 天,感谢阅读至此的你 欢迎点赞、收藏、在...
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int maxDepth(TreeNode* root) { int depth = 0; if(root == NULL)...
10 class Solution { 11 public: 12 int maxDepth(TreeNode* root) { 13 if(!root) return 0; 14 int a=maxDepth(root->left); 15 int b=maxDepth(root->right); 16 return a>b?a+1:b+1; 17 } 18 }; 1. 2. 3. 4. 5. 6. ...
class Solution { public: int maxDepth(TreeNode *root) { if(root == NULL){ return 0; } int left = maxDepth(root->left); int right = maxDepth(root->right); return 1 + max(left,right); } }; 【队列】 /** * Definition for binary tree ...
Leetcode 662. Maximum Width of Binary Tree 文章作者:Tyan 博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution Reference https://leetcode.com/problems/maximum-width-of-binary-tree/description/ [LeetCode]111.Minimum Depth of Binary Tree ...
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value...