美文网首页
2018-03-10 求二叉树深度

2018-03-10 求二叉树深度

作者: 半瓶酱油 | 来源:发表于2018-03-10 09:33 被阅读0次

没什么好说的,遍历一层加1

public int maxDepth(TreeNode root) {

        if (root == null) return 0;

        return 1 + max(maxDepth(root.left), maxDepth(root.right));

    }

    private int max(int p, int q) {

        return p > q ? p : q;

    }

相关文章

网友评论

      本文标题:2018-03-10 求二叉树深度

      本文链接:https://www.haomeiwen.com/subject/hbevfftx.html