剑指offer 39 - 二叉树的深度

1.先遍历二叉树的左子树的深度,然后再遍历二叉树右子树的深度。最后判断左子树和右子树的深度,如果左子树比右子树深则返回左子树深度+1,否则返回右子树深度+1。

2.判断该树是不是平衡树,我们用后序遍历的方式遍历整棵二叉树。在遍历某结点的左右子结点之后,我们可以根据它的左右子结点的深度判断它是不是平衡的,并得到当前结点的深度。当最后遍历到树的根结点的时候,也就判断了整棵二叉树是不是平衡二叉树了。

#include <iostream>
#include<vector>
using namespace std;

struct	BinaryTreeNode
{
	int	m_nValue;
	BinaryTreeNode*		m_pLeft;
	BinaryTreeNode*		m_pRight;
};

BinaryTreeNode* CreateBinaryTreeNode(int	value)
{
	  BinaryTreeNode	*head = new  BinaryTreeNode[sizeof(BinaryTreeNode )];
	  head->m_nValue = value;
	  head->m_pLeft = head->m_pRight	=	NULL;

	  return head;
}

void ConnectTreeNodes(BinaryTreeNode*    root ,BinaryTreeNode*   left, BinaryTreeNode*   right)    
{  
   if(root != NULL)
   {
	   root->m_pLeft = left;
	   root->m_pRight = right;
   }
}   

void DestroyTree(BinaryTreeNode*    root)
{
	if(root !=NULL)
	{
		  BinaryTreeNode* left = root->m_pLeft;
		  BinaryTreeNode* right= root->m_pRight;
		  
		  delete root;
		  root=NULL;
		  DestroyTree(left);
		  DestroyTree( right);
	}	
}

bool  isBalanced(BinaryTreeNode* pRoot,int *depth)     //节点深度作为参数,一边遍历一边判断是否平衡
{
	if(pRoot==NULL)
	{
		*depth=0;
		 return  true;		
	}		
	int left,right;
	if( isBalanced(pRoot->m_pLeft,&left)   &&	isBalanced(pRoot->m_pRight,&right) )
	{
		 int diff = left - right;
        if(diff <= 1 && diff >= -1)  //此处不能写(-1<=diff<=1),错误
		{
			*depth = left>right ? (left+1) :(right+1);      
			return true;
		}
	}	
	return false;
} 

int  TreeDepth(BinaryTreeNode* pRoot)    //节点深度  后序遍历
{  
    if(pRoot==NULL)  
        return 0;         
          
    int nLeft= TreeDepth(pRoot->m_pLeft);     
    int     nRight= TreeDepth(pRoot->m_pRight);    

    return  nLeft>nRight ? (nLeft+1) :(nRight+1);    
  
}   
bool IsBalanced(BinaryTreeNode* pRoot)    //方法2. 需要重复遍历节点多次
{  
    if(pRoot== NULL)  
        return true;  
  
    int nLeftDepth = TreeDepth(pRoot->m_pLeft);  
    int nRightDepth = TreeDepth(pRoot->m_pRight);  
    int diff = nRightDepth-nLeftDepth;  
  
    if (diff>1 || diff<-1)  
        return false;  
  
    return IsBalanced(pRoot->m_pLeft)&&IsBalanced(pRoot->m_pRight);  
}  

// ====================测试代码====================
void Test(char* testName, BinaryTreeNode* pRoot,bool result)
{
    if(testName != NULL)
        printf("%s begins:", testName);

	int depth = 0;
    if(result ==  isBalanced(pRoot,&depth))
		printf("%s\n","passed");
	else
		printf("%s\n","failed");

    printf("\n");
}

//            1
//         /      \
//        2        3
//       / \         \   
//      4  5     	    6  
//			   \        
//				7   
void Test1()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
	BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);

    ConnectTreeNodes(pNode1, pNode2, pNode3);
    ConnectTreeNodes(pNode2, pNode4, pNode5);
	ConnectTreeNodes(pNode3, NULL, pNode6);
    ConnectTreeNodes(pNode5, NULL, pNode7);
   
    Test("Test1", pNode1,true);

    DestroyTree(pNode1);
}

//               1
//              /
//             2
//            /
//           3
//          /
//         4
//        /
//       5
void Test2()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

    ConnectTreeNodes(pNode1, pNode2, NULL);
    ConnectTreeNodes(pNode2, pNode3, NULL);
    ConnectTreeNodes(pNode3, pNode4, NULL);
    ConnectTreeNodes(pNode4, pNode5, NULL);

    Test("Test2",pNode1, false);

    DestroyTree(pNode1);
}

// 1
//  \
//   2
//    \
//     3
//      \
//       4
//        \
//         5
void Test3()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

    ConnectTreeNodes(pNode1, NULL, pNode2);
    ConnectTreeNodes(pNode2, NULL, pNode3);
    ConnectTreeNodes(pNode3, NULL, pNode4);
    ConnectTreeNodes(pNode4, NULL, pNode5);

    Test("Test3",pNode1, false);

    DestroyTree(pNode1);
}

// 树中只有1个结点
void Test4()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    Test("test4",pNode1, true);

    DestroyTree(pNode1);
}

// 树中没有结点
void Test5()
{
	Test("Test5",NULL,true);
}


int main(int argc, char* argv[])
{
    Test1();
	Test2();
    Test3();
    Test4();
    Test5();
    

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值