zoukankan      html  css  js  c++  java
  • 给定一个二叉树,获取该二叉树的宽度深度

    题目:

    Description  

             给定一个二叉树,获取该二叉树的宽度深度。


    Prototype
             int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)
    Input Param 
             head   须要获取深度的二叉树头结点
    Output Param 
             pulWidth   宽度
             pulHeight  高度
    Return Value
             0          成功

             1          失败或其它异常

    分析:使用二叉树的层序遍历,使用队列非常easy的解决这个问题

    代码例如以下:

    int GetBiNodeInfo(BiNode &head, unsigned int *pulWidth, unsigned int *pulHeight)
    {
    	/*在这里实现功能*/
    	if(&head==NULL)
    		return -1;
    	*pulWidth=0;
    	*pulHeight=0;
    	queue<BiNode*> biStack;
    	biStack.push(&head);
    	while(!biStack.empty()){
    		++(*pulHeight);
    		if(biStack.size()>*pulWidth)
    			*pulWidth=biStack.size();
    		int i=biStack.size();
    		while(i>0){
    			BiNode * temp=biStack.front();
    			biStack.pop();
    			if(temp->left!=NULL)
    				biStack.push(temp->left);
    			if(temp->right!=NULL)
    				biStack.push(temp->right);
    			i--;
    		}
    	}
    	printf("pulWidth=%d
     pulHeight=%d
    ",*pulWidth,*pulHeight);
    	return 0;
    }
    


  • 相关阅读:
    任意指定一个key获取该key所处在哪个node节点
    记一次mysql的问题处理@20181225
    Vue 自定义校验规则
    Vue 渲染状态标签
    Vue Token拦截跳转
    vue 组件路由问题
    vue npm运行报错
    Vue复习(一)
    从客户端中检测到有潜在危险的 Request.Form
    EF Core for MySql踩坑(二)
  • 原文地址:https://www.cnblogs.com/zhchoutai/p/6928134.html
Copyright © 2011-2022 走看看