③如果既有右子树又有左子树,那该树的深度就是其左、右子树深度的较大值再加1。 1intmaxDepth(TreeNode*root) {2if(!root)3return0;4intnleft=maxDepth(root->left);5intnright=maxDepth(root->right);6returnnleft>nright?nleft+1:nright+1;7} Java: 1publicintmaxDepth(TreeNode root) {2if(...
* Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ publicclassSolution { publicintmaxDepth(TreeNode root) { if(root ==null) return0; else returnMath.max(maxDepth(root.left) +...
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 中文版描述 给定一个二叉树,找出其最大深度。二叉树的深度为根节点到...
code 递归方法 /** * 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==NULL) return ...
LeetCode Maximum Depth of Binary Tree (求树的深度),题意:给一棵二叉树,求其深度。思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1。1/**2*Definitionforabinarytreenode.3*structTreeNode{4*intval;5...
英文coding面试练习 day3-1 | Leetcode662 Maximum Width of Binary Tree, 视频播放量 29、弹幕量 0、点赞数 0、投硬币枚数 0、收藏人数 0、转发人数 0, 视频作者 Rollwithlife, 作者简介 To remember,相关视频:英文coding面试练习 day3-2 | Leetcode907 Sum of Subarray
My code: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{public intmaxDepth(TreeNode root){returngetDepth(root);}private intgetDepth(TreeNode root){if...
http://www.programcreek.com/2013/02/leetcode-binary-tree-maximum-path-sum-java/ 另外,这道题目的BST条件,似乎没什么用。因为如果全是负数,BST也没帮助了。 ** 总结: pre-order -> top-down post-order -> bottom-up in-order -> ? level-order -> bfs ...
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. 即求二叉树的(最大)深度。
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 ...