intmaxDepth(TreeNode*root){if(root==NULL)return0;queue<TreeNode*>btQueue;//使用队列进行层序遍历btQueue.push(root);int count=1;//记录当前层的节点个数int depth=0;//记录当前遍历的深度while(!btQueue.empty()){TreeNode*currentNode=btQueu
int maxDepth(TreeNode *root) { if(root==NULL) return 0; return max(maxDepth(root->left),maxDepth(root->right))+1; } }; 队列 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL),...
* int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ publicclassSolution{ publicintmaxDepth(TreeNode root){ if(root !=null) { intrightResult = maxDepth(root.left) +1; intleftResult = maxDepth(root.right) +1; returnleftResult > rightResult ?
* 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) { if (root==0) { return 0; } int left=m...
{ int maxValue = 0; public int maxDepth(TreeNode root) { // 设置一个变量maxValue,用于存储该树的最大深度,遍历求每一个节点的深度并与maxValue进行比较,大于maxValue则替换,小于或等于则保持不变 getDeepth(root); return maxValue; } public int getDeepth(TreeNode root) { if (root == null) ...
id=104 lang=javascript * * [104] Maximum Depth of Binary Tree *//** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {TreeNode} root * @return {number} */var maxDepth = functi...
max(depth, current_depth); stack.add(new Pair(root.left, current_depth + 1)); stack.add(new Pair(root.right, current_depth + 1)); } } return depth; } }; Python 代码实现 class Solution: def maxDepth(self, root): """ :type root: TreeNode :rtype: int """ stack = [] if ...
Can you solve this real interview question? Maximum Depth of Binary Tree - Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest lea
int maxDepth(TreeNode* root) { if(root==NULL) return 0; deque<TreeNode*> q; q.push_back(root); int deep=0; while(!q.empty()) { deep++; int num=q.size(); for(int i=1;i<=num;i++) { TreeNode* p=q.front();
{ public: int maxDepth(TreeNode* root) { if (!root) { return 0; } int Result = 1; int Left = maxDepth(root->left); int Right = maxDepth(root->right); if (Left * Right) { Result += Left < Right? Right : Left; } else { Result += Right + Left; } return Result; } ...