GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/DiameterOfBinaryTree.java at master · Tulpen/Leetcode-Java Expand file tree/ Copy pathDiameterOfBinaryTree.javaMore file actions 31 lines (25 loc) · 707 Bytes / Copy pathDiameterOfBinaryTree.javaFile metadata and controls31 lines (25 loc) · 707 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class DiameterOfBinaryTree { /** * 这题和 124. Binary Tree Maximum Path Sum比较像 */ public int diameterOfBinaryTree(TreeNode root) { if (root == null) { return 0; } return dfs(root, new int[1]) - 1; } /** * len表示带上root的最大深度 */ private int dfs(TreeNode root, int[] len) { if (root == null) { return 0; } int[] lt = new int[1]; int[] rt = new int[1]; int left = dfs(root.left, lt); int right = dfs(root.right, rt); len[0] = Math.max(lt[0], rt[0]) + 1; return Math.max(Math.max(left, right), lt[0] + rt[0] + 1); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |