③如果既有右子树又有左子树,那该树的深度就是其左、右子树深度的较大值再加1。 1intmaxDepth(TreeNode*root) {2if(!root)3return0;4intnleft=maxDepth(root->left);5intnright=maxDepth(root->right);6returnnleft>nright?nleft+1:nright+1;7} Java: 1
* 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 中文版描述 给定一个二叉树,找出其最大深度。二叉树的深度为根节点到...
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 maxDepth(TreeNode root) { if (root == null) return 0; int h1 = maxDepth(root.le...
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
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A leaf is a node with no children. Example 1 Given binary tree[3,9,20,null,null,15,7],3/\920/\157returnits depth=3. ...
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 ...
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
* Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: Solution(){depth=0;lastDepth=0;} ...