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: Given binary tree[3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its depth = 3. 求二叉树的最大深度问...
③如果既有右子树又有左子树,那该树的深度就是其左、右子树深度的较大值再加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(...
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. 即求二叉树的(最大)深度。 有两种思路: 一是使用递归, 二是使用二叉树层序遍历。
* Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ import java.util.Vector; class Solution { public int maxDepth(TreeNode root) { int dep = 0; if (null != root) { Vector...
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 中文版描述 给定一个二叉树,找出其最大深度。二叉树的深度为根节点到...
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: Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 ...
104. 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. 题目翻译 给定一个二叉树,找到它的最大深度。 最大深度是沿着从根节点到最远叶节点的...
0111-minimum-depth-of-binary-tree.cpp 0115-distinct-subsequences.cpp 0116-populating-next-right-pointers-in-each-node.cpp 0117-populating-next-right-pointers-in-each-node-ii.cpp 0118-pascals-triangle.cpp 0120-triangle.cpp 0121-best-time-to-buy-and-sell-stock.cpp 0122-best-time-to-buy-and-se...
0110-balanced-binary-tree.cpp 0111-minimum-depth-of-binary-tree.cpp 0115-distinct-subsequences.cpp 0116-populating-next-right-pointers-in-each-node.cpp 0117-populating-next-right-pointers-in-each-node-ii.cpp 0118-pascals-triangle.cpp 0120-triangle.cpp 0121-best-time-to-buy-and-sell-stock.cpp ...
用两个栈的方法很巧妙。同时暂存头结点的depth -> temp BFS My code: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * }