【Leetcode】Maximum Depth of Binary Tree

来源:互联网 发布:php quot 转成符号 编辑:程序博客网 时间:2024/05/16 05:21

题目链接:https://leetcode.com/problems/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 h1 = maxDepth(root.left);int h2 = maxDepth(root.right);return h1 > h2 ? h1 + 1 : h2 + 1;}


0 0
原创粉丝点击