LeetCode 543: Diameter of Binary Tree
Posted keepshuatishuati
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 543: Diameter of Binary Tree相关的知识,希望对你有一定的参考价值。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { private int maxValue = 0; public int diameterOfBinaryTree(TreeNode root) { if (root == null) { return 0; } getDia(root); return maxValue - 1; } private int getDia(TreeNode root) { if (root == null) { return 0; } int left = getDia(root.left); int right = getDia(root.right); maxValue = Math.max(left + right + 1, maxValue); return Math.max(left, right) + 1; } }
以上是关于LeetCode 543: Diameter of Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 543. Diameter of Binary Tree 解题笔记
leetcode_easy543. Diameter of Binary Tree
[leetcode-543-Diameter of Binary Tree]
Leetcode 543: Diameter of Binary Tree