【LeetCode从零单刷】Maximum Depth of Binary Tree

没错我就是伍声2009的粉丝,从今天起,模仿《从零单排》系列,菜鸡单刷LeetCode!

题目:

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 binary tree
 * 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->left == NULL && root->right == NULL)
        {
            return 1;
        }
        if(root->left != NULL && root->right == NULL)
        {
            return (1+maxDepth(root->left));
        }
        if(root->right != NULL && root->left == NULL)
        {
            return (1+maxDepth(root->right));
        }
        else
        {
            return (1+maxDepth(root->right))>(1+maxDepth(root->left))?(1+maxDepth(root->right)):(1+maxDepth(root->left));
        }
    }
};

Runtime Error 了……原因是,当输入空树时无法处理。但是改了之后还是还有通过……原因是超时。想了半天,问题在这里:

return (1+maxDepth(root->right))>(1+maxDepth(root->left))?(1+maxDepth(root->right)):(1+maxDepth(root->left));

这样不就是把子树的 maxDepth 求解了四次么?正确方法应该将子树高度保存下来,这样只需要计算两次,节省一半时间。得到 AC:

/**
 * Definition for binary tree
 * 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 0;
        }
        if(root->left == NULL && root->right == NULL)
        {
            return 1;
        }
        if(root->left != NULL && root->right == NULL)
        {
            return (1+maxDepth(root->left));
        }
        if(root->right != NULL && root->left == NULL)
        {
            return (1+maxDepth(root->right));
        }
        else
        {
            int l = maxDepth(root->left);
            int r = maxDepth(root->right);
            return (1+r)>(1+l)?(1+r):(1+l);
        }
    }
};
时间: 2024-10-12 15:09:38

【LeetCode从零单刷】Maximum Depth of Binary Tree的相关文章

[LeetCode][Java]Maximum Depth of Binary Tree

https://leetcode.com/problems/maximum-depth-of-binary-tree/ 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 1

【LeetCode】 Maximum Depth of Binary Tree

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. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

leetcode -day24 Maximum Depth of Binary Tree & Binary Tree Zigzag Level Order Traversal

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. class Solution { public: int maxDepth(TreeNode *root) { inM

LeetCode OJ - Minimum && Maximum Depth of Binary Tree

这两道题用递归的解法都很简单,只是稍有不同. 下面是AC代码: 1 /** 2 * Given a binary tree, find its minimum depth. 3 * the minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 4 * @param root 5 * @return 6 */ 7 public in

Maximum Depth of Binary Tree leetcode java

题目: 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. 题解: 递归解法: 1     public int maxDepth(TreeNode root) {2         if(root==null)3         

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. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://

Leetcode 树 Maximum Depth of Binary Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Maximum Depth of Binary Tree Total Accepted: 16605 Total Submissions: 38287 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the ro

【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. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

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. 此题和求二叉树的最短路径几乎一模一样. public int maxDepth(TreeNode root) { if (root == null) return 0; int a

我也来刷LeetCode——0、Maximum Depth of Binary Tree(开篇)

作为一个非科班出身的程序员,还是很有必要刷刷题的.之前有个大牛说,菜鸟刷题可以从AC率高的刷起,这样可以快速培养信心.将LeetCode的题目按照AC率从高到低排序,第一道题目为 [Maximum Depth of Binary Tree]. 从题目就可以看出,是求二叉树的最大深度.深度的概念,是指从根节点到叶子节点的这条路径上的总节点数. 要求二叉树的最大深度,只需要求出左子树的深度和右子树的深度,取两者最大值再加 1 ,即为二叉树的最大深度. 若该二叉树为空,返回 0 . 然后就是将算法翻译