zoukankan      html  css  js  c++  java
  • 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 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.


    The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two leaves in the tree. The diagram below shows two trees each with diameter nine, the leaves that form the ends of a longest path are shaded (note that there is more than one path in each tree of length nine, but no path longer than nine nodes).

    The diameter of a tree T is the largest of the following quantities:

    * the diameter of T’s left subtree
    * the diameter of T’s right subtree
    * the longest path between leaves that goes through the root of T (this can be computed from the heights of the subtrees of T)

    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. }






  • 相关阅读:
    MySQL3:索引
    MySQL4:存储过程和函数
    MySQL2:四种MySQL存储引擎
    MySQL1:MySQL函数汇总
    web.xml详解
    SharePoint 多行文本字段设置默认值
    SharePoint Online 自定义Modern UI表单
    Server-side activities have been updated. You need to restart SharePoint Designer to use the updated version of activities.
    计算请假天数JavaScript方法
    SharePoint 2019 离线安装准备工具
  • 原文地址:https://www.cnblogs.com/xiejunzhao/p/ac46feea266ab08377ddd3fff9b2c61e.html
Copyright © 2011-2022 走看看