1. 二叉树的直径(中等)
1. 二叉树的直径(中等)地址: https://leetcode-cn.com/problems/diameter-of-binary-tree/
2021/01/23
做题反思:
class Solution { int maxDiameter = 0; public int diameterOfBinaryTree(TreeNode root) { maxDepth(root); return maxDiameter; } int maxDepth(TreeNode root) { if (root == null) { return 0; } int maxLeft = maxDepth(root.left); int maxRight = maxDepth(root.right); int myDiameter = maxLeft + maxRight; maxDiameter = Math.max(maxDiameter, myDiameter); return Math.max(maxLeft, maxRight) + 1; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)