LeetCode笔记: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.

大意:

给出一个二叉树,找到其最大的深度。
最大深度是指从根节点到最远的叶子节点的最长距离的节点数。

思路:

要探索二叉树的深度,用递归比较方便。我们题目要求的函数返回根节点的深度,那么就做到对二叉树上每个节点调用此函数都返回其作为根节点看待时的深度。比如,所有叶子节点的深度都是1,再往上就是2、3...一直到root根节点的返回值就是最大的深度。
对于每个节点,我们先判断其本身是否是节点,如果是一个空二叉树,那么就应该返回0。
然后,我们定义两个变量,一个左节点深度,一个右节点深度。我们分别判断其有无左节点和右节点,两种节点中的做法都是一样的,假设没有左节点,那么就左节点深度变量就是1,有左节点的话,左节点深度变量就是对左节点调用此函数返回的结果加1;对右节点也做同样的操作。
最后比较左节点深度和右节点深度,判断谁比较大,就返回哪个变量。这样就能一层一层地递归获取最大深度了。

代码(Java):

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxDepth(TreeNode root) {
        if (root != null) {// 有此节点
            int rightResult;
            int leftResult;
            if (root.left != null) {// 有左节点
                leftResult = maxDepth(root.left) + 1;
            } else {// 无左节点
                leftResult = 1;
            }
            if (root.right != null) {// 有右节点
                rightResult = maxDepth(root.right) + 1;
            } else {// 无右节点
                rightResult = 1;
            }
            // 判断哪边更深,返回更深的深度
            return leftResult > rightResult ? leftResult : rightResult;
        } else {// 无此节点,返回0
            return 0;
        }
    }
}

不过我们稍加思考一下,就可以进一步简略一下代码。因为我们代码里对于root为null的情况下返回的是0,那其实没有左节点时,对齐使用函数返回的也会是0,加1的话就是我们需要的1了,所以其实不用判断有无左节点,右节点也是一样。所以可以简化如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxDepth(TreeNode root) {
        if (root != null) {
            int rightResult = maxDepth(root.left) + 1;
            int leftResult = maxDepth(root.right) + 1;
            return leftResult > rightResult ? leftResult : rightResult;
        } else {
            return 0;
        }
    }
}

也可以额外写个函数,在参数里传递深度。

// C++:
/**
 * 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 depth(TreeNode* node, int level) {
        if (node == NULL) return level-1;
        
        int left = depth(node->left, level+1);
        int right = depth(node->right, level+1);
        
        return max(left, right);
    }
    
    int maxDepth(TreeNode* root) {
        return depth(root, 1);
    }
};

合集:https://github.com/Cloudox/LeetCode-Record


查看作者首页

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 参考两篇其他bolg总结的二叉树:https://github.com/xy7313/lintcode/blob/...
    暗黑破坏球嘿哈阅读 2,423评论 0 1
  • 树的概述 树是一种非常常用的数据结构,树与前面介绍的线性表,栈,队列等线性结构不同,树是一种非线性结构 1.树的定...
    Jack921阅读 4,509评论 1 31
  • 94. Binary Tree Inorder Traversal 中序 inorder:左节点->根节点->右节...
    Morphiaaa阅读 595评论 0 0
  • 二叉树的定义#### 二叉树是n(n>=0)个具有相同类型的元素的有限集合,当n=0时称为空二叉树,当n>0时,数...
    kylinxiang阅读 1,463评论 0 2
  • 感悟:昨天岳父来了,喝了酒和我聊了好久,提起当初我结婚时说的承诺,我早就忘了,可他还记得,这个世界上父爱是最伟大的...
    liuxu火火火阅读 163评论 0 0