543. Diameter of Binary Tree

题目:

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Example:
Given a binary tree

          1
         /         2   3
       / \
      4   5

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.

链接:https://leetcode.com/problems/diameter-of-binary-tree/#/description

4/1/2017

10ms

 1 public class Solution {
 2     int maxDiff = 0;
 3     public int diameterOfBinaryTree(TreeNode root) {
 4         heightOfBinaryTree(root);
 5         return maxDiff;
 6     }
 7     private int heightOfBinaryTree(TreeNode root) {
 8         if (root == null) return 0;
 9         int heightL = heightOfBinaryTree(root.left);
10         int heightR = heightOfBinaryTree(root.right);
11         if (heightL + heightR > maxDiff) maxDiff = heightL + heightR;
12         return Math.max(heightL, heightR) + 1;
13     }
14 }

更多讨论:

https://discuss.leetcode.com/category/696/diameter-of-binary-tree

时间: 2024-10-19 03:20:37

543. Diameter of Binary Tree的相关文章

【leetcode_easy】543. Diameter of Binary Tree

problem 543. Diameter of Binary Tree 参考 1. Leetcode_easy_543. Diameter of Binary Tree; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10943221.html

LeetCode 543. Diameter of Binary Tree (二叉树的直径)

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary tr

543. Diameter of Binary Tree(两节点的最长路径)

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary t

[LC] 543. Diameter of Binary Tree

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary tr

543 Diameter of Binary Tree 二叉树的直径

给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树          1         / \        2   3       / \           4   5    返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3].注意:两结点之间的路径长度是以它们之间边的数目表示.详见:https://leetcode.com/problems/diameter-of-binary-t

[leetcode]543. Diameter of Binary Tree二叉树的直径

题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 int res = 1; public int diameterOfBinaryTree(TreeNode root) { helper(root); return res-1; } public int[] helper(TreeNode root) { //两个量分别是节点深度,节点最大dia

543. 二叉树的直径 Diameter of Binary Tree

/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { public int DiameterOfBinaryTree(TreeNode root)

[LeetCode] Diameter of Binary Tree 二叉树的直径

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longestpath between any two nodes in a tree. This path may or may not pass through the root. Example:Given a binary tr

leetcode543 - Diameter of Binary Tree - easy

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root. Example: Given a binary