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

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     }
复制代码
{
    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);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值