zoukankan      html  css  js  c++  java
  • 【剑指offer】推断二叉树平衡

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/27242575

        题目:输入一个二叉树的根节点,推断该树是不是平衡二叉树。

    假设某二叉树中随意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。

        剑指offer上给的另外一种思路,用后序遍历真的是将递归发挥的淋漓尽致。先遍历节点的左右子树。左右子树都平衡才来推断该节点是否平衡。假设左右子树中有不平衡的,则直接返回false,避免了从上往下逐个节点地计算深度带来的反复遍历节点。

        代码例如以下:

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct BTNode
    {
    	char data;
    	struct BTNode *pLchild;
    	struct BTNode *pRchild;
    }BTNode, *BTree;
    
    BTree create_tree();
    bool IsBalanced(BTree,int *);
    bool IsBalanced(BTree);
    
    int main()
    {
    	BTree pTree = create_tree();
    	if(IsBalanced(pTree))
    		printf("Balanced
    ");
    	else
    		printf("Not Balanced
    ");
    
    	return 0;
    }
    
    BTree create_tree()
    {
    	BTree pA = (BTree)malloc(sizeof(BTNode));
    	BTree pB = (BTree)malloc(sizeof(BTNode));
    	BTree pD = (BTree)malloc(sizeof(BTNode));
    	BTree pE = (BTree)malloc(sizeof(BTNode));
    	BTree pC = (BTree)malloc(sizeof(BTNode));
    	BTree pF = (BTree)malloc(sizeof(BTNode));
    
    
    	pA->data = 'A';
    	pB->data = 'B';
    	pD->data = 'D';
    	pE->data = 'E';
    	pC->data = 'C';
    	pF->data = 'F';
    	
    	pA->pLchild = pB;
    	pA->pRchild = pC;
    	pB->pLchild = pD;
    	pB->pRchild = pE;
    	pD->pLchild = NULL;
    	pD->pRchild = NULL;
    	pE->pLchild = pE->pRchild = NULL;
    	pC->pLchild = NULL;
    	pC->pRchild = pF;
    	pF->pLchild = pF->pRchild = NULL;	
    	
    	return pA;
    }
    
    
    /*
    兴许递归遍历推断二叉树是否平衡
    */
    bool IsBalanced(BTree pTree,int *depth)
    {
    	if(pTree == NULL)
    	{
    		*depth = 0;
    		return true;
    	}
    
    	int leftDepth,rightDepth;
    	if(IsBalanced(pTree->pLchild,&leftDepth) && IsBalanced(pTree->pRchild,&rightDepth))
    	{
    		int diff = leftDepth-rightDepth;
    		if(diff<=1 && diff>=-1)
    		{
    			*depth = (leftDepth>rightDepth ? leftDepth:rightDepth) + 1;
    			return true;
    		}
    	}
    	return false;
    }
    
    /*
    封装起来
    */
    bool IsBalanced(BTree pTree)
    {
    	int depth = 0;
    	return IsBalanced(pTree,&depth);
    }
        測试结果:



  • 相关阅读:

    链表
    Codeforces 1290A/1291C
    Codeforces 1291B
    Codeforces 1291A
    Codeforces 1295C
    Codeforces 1295B
    ZJNU 2356
    ZJNU 2354
    ZJNU 2353
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6749034.html
Copyright © 2011-2022 走看看