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

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left;
  6. * public TreeNode right;
  7. * public TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public int DiameterOfBinaryTree(TreeNode root) {
  12. if (root == null)
  13. return 0;
  14. /* get the height of left and right sub trees */
  15. int lheight = height(root.left);
  16. int rheight = height(root.right);
  17. /* get the diameter of left and right subtrees */
  18. int ldiameter = DiameterOfBinaryTree(root.left);
  19. int rdiameter = DiameterOfBinaryTree(root.right);
  20. /* Return max of following three
  21. 1) Diameter of left subtree
  22. 2) Diameter of right subtree
  23. 3) Height of left subtree + height of right subtree + 1 */
  24. return Math.Max(lheight + rheight, Math.Max(ldiameter, rdiameter));
  25. }
  26. /*The function Compute the "height" of a tree. Height is the
  27. number f nodes along the longest path from the root node
  28. down to the farthest leaf node.*/
  29. public int height(TreeNode node) {
  30. if (node == null)
  31. return 0;
  32. /* If tree is not empty then height = 1 + max of left
  33. height and right heights */
  34. return (1 + Math.Max(height(node.left), height(node.right)));
  35. }
  36. }

null

时间: 2024-08-06 16:06:45

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 二叉树的直径

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

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

Level: ??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. Exam

[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

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 bina

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. 二叉树的直径

题目描述 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / 2 3 / \ 4 5 返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]. 注意:两结点之间的路径长度是以它们之间边的数目表示. 分析 感觉这个题最精髓的就是这两行代码了, 因为题目要求我们算的直径考虑的是最长的边,而不是最长的 path 的node数目,如果是统计最长的 path 的 node 数目,每次递归的时