标签:全局变量 返回 nod 计算 for pre represent The def
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { int maxLen = 0; public int diameterOfBinaryTree(TreeNode root) { calHelper(root); return maxLen; } /* * return the current depth. leaf node has depth of 1. */ private int calHelper(TreeNode root) { if (root == null) { return 0; } int leftDepth = calHelper(root.left); int rightDepth = calHelper(root.right); maxLen = Math.max(maxLen, leftDepth + rightDepth); return Math.max(leftDepth, rightDepth) + 1; } }
leetcode543 - Diameter of Binary Tree - easy
标签:全局变量 返回 nod 计算 for pre represent The def
原文地址:https://www.cnblogs.com/jasminemzy/p/9771834.html