104. Maximum Depth of Binary Tree

    xiaoxiao2021-03-25  57

    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 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) { int depth=0; if(root==null) return 0; depth++; int leftDepth=maxDepth(root.left); int rightDepth=maxDepth(root.right); depth+=Math.max(leftDepth, rightDepth); return depth; } }

    转载请注明原文地址: https://ju.6miu.com/read-36016.html

    最新回复(0)