LeetCode: Maximum Depth of Binary Tree 题目如下: 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. 即求二叉树的(最大)深度。 有两种思路: 一是使用递归, 二是使用二叉树层序遍历。
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),...
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 解法1:很简单的DFS递归。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x...
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 天,感谢阅读至此的你 欢迎点赞、收藏、在...
来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ 解题思路 递归DFS(深度优先搜索 Depth-First-Search) : 首先,我们要知道一棵树的最大深度在逻辑上怎么求? 树的最大深度 = 根节点的高度(根本身为 1 )+ 左右子树的最大深度中的较大者。
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...
if(root==NULL) return 0; return max(1+maxDepth(root->left), 1+maxDepth(root->right)); } }; 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 参考 1.Maximum Depth of Binary Tree; 完...
简介: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.二话不说,递归求解,一次通过。public int Given a binary tree, find its maximum depth. ...
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. 【代码】 【递归】 时间复杂度为O(n) 空间复杂度为O(logn) /** * Definition for binary tree ...