The diameter of a binary tree

The diameter of a binary tree [No. 40]

分类: 二叉树   348人阅读  评论(0)  收藏  举报

Definition: 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 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)
[java]  view plain copy
  1. /* Function to get diameter of a binary tree */  
  2.    static int diameter(Node root) {  
  3.     if (root == null) {  
  4.         return 0;  
  5.     }  
  6.       
  7.     int lHeight = height(root.leftChild);  
  8.     int rHeight = height(root.rightChild);  
  9.       
  10.     int lDiameter = diameter(root.leftChild);  
  11.     int rDiameter = diameter(root.rightChild);  
  12.       
  13.     return max(lHeight + rHeight + 1, max(lDiameter, rDiameter));  
  14.       
  15.    }  
  16.      
  17.    /*  The function Compute the "height" of a tree. Height is the 
  18.    number f nodes along the longest path from the root node 
  19.    down to the farthest leaf node.*/  
  20.    static int  height(Node root) {  
  21.     if (root == null) {  
  22.         return 0;  
  23.     }  
  24.       
  25.     return 1 + max(height(root.leftChild), height(root.rightChild));  
  26.    }  

The complexity of the program is O(n^2), because the height function of called everytime when we go through the tree.

There is another solution and the complexity is O(N). The main idea of this approach is that the node stores its left child's and right child's maximum diameter if the node's child is the "root", therefore, there is no need to recursively call the height method. The drawback is we need to add two extra variables in the node class.

[java]  view plain copy
  1. int findMaxLen(Node root) {  
  2.        
  3.     int nMaxLen = 0;  
  4.     if (root == nullreturn 0;  
  5.       
  6.     if (root.leftChild == null) {  
  7.         root.nMaxLeft = 0;  
  8.     }  
  9.       
  10.     if (root.rightChild == null) {  
  11.         root.nMaxRight = 0;  
  12.     }  
  13.       
  14.     if (root.leftChild != null) {  
  15.         findMaxLen(root.leftChild);  
  16.     }  
  17.       
  18.     if (root.rightChild != null) {  
  19.         findMaxLen(root.rightChild);  
  20.     }  
  21.       
  22.     if (root.leftChild != null) {  
  23.         int nTempMaxLen = 0;  
  24.         nTempMaxLen = (root.leftChild.nMaxLeft > root.leftChild.nMaxRight) ?   
  25.             root.leftChild.nMaxLeft : root.leftChild.nMaxRight;  
  26.         root.nMaxLeft = nTempMaxLen + 1;  
  27.     }  
  28.       
  29.     if (root.rightChild != null) {  
  30.         int nTempMaxLen = 0;  
  31.         nTempMaxLen = (root.rightChild.nMaxLeft > root.rightChild.nMaxRight) ?   
  32.             root.rightChild.nMaxLeft : root.rightChild.nMaxRight;  
  33.         root.nMaxRight = nTempMaxLen + 1;  
  34.     }  
  35.       
  36.     if (root.nMaxLeft + root.nMaxRight > nMaxLen) {  
  37.         nMaxLen = root.nMaxLeft + root.nMaxRight;  
  38.     }  
  39.       
  40.     return nMaxLen;  
  41. }  
参考:编程之美
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值