0
点赞
收藏
分享

微信扫一扫

LeetCode-104-二叉树的最大深度

月白色的大狒 2021-09-28 阅读 64
LeetCode

二叉树的最大深度

解法一:递归
package com.kaesar.leetcode;

public class LeetCode_104 {
    public static int result = 0;

    public static int maxDepth(TreeNode root) {
        maxDepth(root, 0);
        return result;
    }

    public static void maxDepth(TreeNode root, int curDepth) {
        if (root == null) {
            if (curDepth > result) {
                result = curDepth;
            }
            return;
        }
        curDepth++;
        maxDepth(root.left, curDepth);
        maxDepth(root.right, curDepth);
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        root.right = new TreeNode(2);
        System.out.println(maxDepth(root));
    }
}
举报

相关推荐

0 条评论