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();//每次循环出队一...
1、minimum depth of binary tree 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) {}8* };9*/10classSolution {11public:12intminDepth(TreeNode *root) {13if(root ==NU...
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),...
Python3代码 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = NoneclassSolution:defminDepth(self,root:TreeNode)->int:# solution one: DFS# 根为空ifnotroot:return0# 左右子树都为空ifnotroot.leftandnotroot...
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 leaf node. 英文版地址 leetcode.com/problems/m 中文版描述 给定一个二叉树,找出其最大深度。二叉树的深度为根节点到...
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 天,感谢阅读至此的你 欢迎点赞、收藏、在...
https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/submissions/给定一个二叉树,找出其最小深度。最小深度是从根节点到最近叶子节点的最短路径上的节点数量。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树 [3,9,20,null,null,15,7],...
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
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 这题跟上题几乎一样,区别在于需要求出根节点到最近的叶子节点的深度,我们仍然使用遍历的方式。
int mright=minDepth(root->right); if(mleft==0) return 1+mright; else if(mright==0) return 1+mleft; else return min(mleft,mright)+1; } }; 二、 * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; ...