求二叉树的深度和宽度平衡最大距离

来源:互联网 发布:压缩软件 mac 编辑:程序博客网 时间:2024/05/21 09:51

http://www.cnblogs.com/xudong-bupt/p/4036190.html

这个是常见的对二叉树的操作。总结一下:

设节点的数据结构,如下:

复制代码
1 class TreeNode {2     char val;3     TreeNode left = null;4     TreeNode right = null;5 6     TreeNode(char _val) {7         this.val = _val;8     }9 }
复制代码

 1.二叉树深度

  这个可以使用递归,分别求出左子树的深度、右子树的深度,两个深度的较大值+1即可。

复制代码
 1     // 获取最大深度 2     public static int getMaxDepth(TreeNode root) { 3         if (root == null) 4             return 0; 5         else { 6             int left = getMaxDepth(root.left); 7             int right = getMaxDepth(root.right); 8             return 1 + Math.max(left, right); 9         }10     }
复制代码

2.二叉树宽度

  使用队列,层次遍历二叉树。在上一层遍历完成后,下一层的所有节点已经放到队列中,此时队列中的元素个数就是下一层的宽度。以此类推,依次遍历下一层即可求出二叉树的最大宽度。

复制代码
 1 // 获取最大宽度 2     public static int getMaxWidth(TreeNode root) { 3         if (root == null) 4             return 0; 5  6         Queue<TreeNode> queue = new ArrayDeque<TreeNode>(); 7         int maxWitdth = 1; // 最大宽度 8         queue.add(root); // 入队 9 10         while (true) {11             int len = queue.size(); // 当前层的节点个数12             if (len == 0)13                 break;14             while (len > 0) {// 如果当前层,还有节点15                 TreeNode t = queue.poll();16                 len--;17                 if (t.left != null)18                     queue.add(t.left); // 下一层节点入队19                 if (t.right != null)20                     queue.add(t.right);// 下一层节点入队21             }22             maxWitdth = Math.max(maxWitdth, queue.size());23         }24         return maxWitdth;25     }
复制代码


3 判断是否为平衡二叉树
低效率算法
bool IsBalanced(BinaryTreeNode *pRoot)
{
    if (pRoot == NULL);
        return true;
    
    int left = TreeDepth(pRoot->lchild);
    int right = TreeDepth(pRoot->rchild);
    int diff = left - right;
    if (diff > 1 || diff < -1)
        return false;
    
     return IsBalanced(pRoot->lchild) && IsBalanced(pRoot->rchild);
}


所有节点只遍历一遍的算法

bool IsBalanced(BinaryTreeNode *pRoot, int *pDepth)

{

    if (pRoot == NULL)

   {

         *pDepth = 0;//当前节点高度为0,从叶子节点往上算高度

          return true;

   }


    int left, right;

   if (IsBalanced(pRoot->lchild, &left) && IsBalanced(pRoot->rchild, &right))

   {

         int diff = left - right;

         if (diff <= 1 && diff >= -1)

         {

              *pDepth = 1 + (left > right ? left : right);

                return true;

         }

   }

   return false;

}


4

方法一:求二叉树中节点的最大距离,等同于“计算每个节点的左子树和右子树的高度和,取最大值”

  1. int height(BiTree T, int &max)  
  2. {  
  3.     if(!T)  
  4.         return -1;  
  5.     int i=height(T->lchild,max)+1;  
  6.     int j=height(T->rchild,max)+1;  
  7.     int distance=i+j;  
  8.     if(max<distance) max=distance;  
  9.     return i>j?i:j;  
  10. }  
  11.   
  12. int maxdistance(BiTree T)  
  13. {  
  14.     int maxdis=0;  
  15.     height(T, maxdis);  
  16.     return maxdis;  
  17. }  

方法二:见编程之美(动态规划),修改3的方法,从叶子节点开始累加高度

所有节点只遍历一遍的算法

void getDepth(BinaryTreeNode *pRoot, int *pDepth)

{

    if (pRoot == NULL)

   {

         *pDepth = 0;//当前节点高度为0,从叶子节点往上算高度

          return true;

   }


   int left, right;

   getDepth(pRoot->lchild, &left);

   getDepth(pRoot->rchild, &right);

   gMaxLen = gMaxLen > (left + right) ? (gMaxLen : (left + right));

   *pDepth = 1 + (left > right ? left : right);

}



0 0
原创粉丝点击