已知有序数组求最小深度二叉树
2013-09-16 14:16
337 查看
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
typedef struct _tree_node
{
int m_nValue;
struct _tree_node *m_pLeft;
struct _tree_node *m_pRight;
}TreeNode;
TreeNode* createNode(int value)
{
TreeNode* pNode=(TreeNode *)malloc(sizeof(TreeNode));
assert(pNode!=NULL);
pNode->m_nValue=value;
pNode->m_pLeft=NULL;
pNode->m_pRight=NULL;
return pNode;
}
TreeNode* arrToTree(int *arr,int start,int end)
{
TreeNode *pHead=NULL;
if(end>=start)
{
int mid=(start+end)>>1;
pHead=createNode(arr[mid]);
pHead->m_pLeft=arrToTree(arr,start,mid-1);
pHead->m_pRight=arrToTree(arr,mid+1,end);
}
return pHead;
}
void printTree(TreeNode *pHead)
{
if(pHead==NULL)
return;
printTree(pHead->m_pLeft);
printf("%d ",pHead->m_nValue);
printTree(pHead->m_pRight);
}
int getTreeHeight(TreeNode *pHead)
{
if(pHead==NULL)
return 0;
return getTreeHeight(pHead->m_pLeft)>getTreeHeight(pHead->m_pRight)?getTreeHeight(pHead->m_pLeft)+1:getTreeHeight(pHead->m_pRight)+1;
}
int main()
{
int arr[]={0,1,2,3,4,5,6,7,8,9};
int len=sizeof(arr)/sizeof(arr[0]);
TreeNode *pHead=NULL;
pHead=arrToTree(arr,0,len-1);
printTree(pHead);
int hight=getTreeHeight(pHead);
printf("\n%d",hight);
system("pause");
return 0;
}
#include<assert.h>
#include<stdlib.h>
typedef struct _tree_node
{
int m_nValue;
struct _tree_node *m_pLeft;
struct _tree_node *m_pRight;
}TreeNode;
TreeNode* createNode(int value)
{
TreeNode* pNode=(TreeNode *)malloc(sizeof(TreeNode));
assert(pNode!=NULL);
pNode->m_nValue=value;
pNode->m_pLeft=NULL;
pNode->m_pRight=NULL;
return pNode;
}
TreeNode* arrToTree(int *arr,int start,int end)
{
TreeNode *pHead=NULL;
if(end>=start)
{
int mid=(start+end)>>1;
pHead=createNode(arr[mid]);
pHead->m_pLeft=arrToTree(arr,start,mid-1);
pHead->m_pRight=arrToTree(arr,mid+1,end);
}
return pHead;
}
void printTree(TreeNode *pHead)
{
if(pHead==NULL)
return;
printTree(pHead->m_pLeft);
printf("%d ",pHead->m_nValue);
printTree(pHead->m_pRight);
}
int getTreeHeight(TreeNode *pHead)
{
if(pHead==NULL)
return 0;
return getTreeHeight(pHead->m_pLeft)>getTreeHeight(pHead->m_pRight)?getTreeHeight(pHead->m_pLeft)+1:getTreeHeight(pHead->m_pRight)+1;
}
int main()
{
int arr[]={0,1,2,3,4,5,6,7,8,9};
int len=sizeof(arr)/sizeof(arr[0]);
TreeNode *pHead=NULL;
pHead=arrToTree(arr,0,len-1);
printTree(pHead);
int hight=getTreeHeight(pHead);
printf("\n%d",hight);
system("pause");
return 0;
}
相关文章推荐
- Leetcode 最小二叉树深度和删除数组重复元素
- Chapter 4 | Trees and Graphs--有序数组构建最小高度的二叉树
- Chapter 4 | Trees and Graphs--有序数组构建最小高度的二叉树
- 给定一个有序数组(递增),写程序构建一棵具有最小高度的二叉树。
- 【IT笔试面试题整理】有序数组生成最小高度二叉树
- (016)给定一个有序数组(递增),写程序构建一棵具有最小高度的二叉树(keep it up)
- leetcode之二叉树类之二叉树深度系列-----104/111/110/108/109 二叉树最大/最小深度/AVL树的判断和由有序序列生成(牵扯分治相关,OJ105/106)
- (016)给定一个有序数组(递增),敲代码构建一棵具有最小高度的二叉树(keep it up)
- 给定一个有序数组(递增),构建一棵具有最小高度的二叉树
- 给定一个有序数组(递增),构建一棵具有最小高度的二叉树
- 微软等数据结构+算法面试100题(5)--怎样编写一个程序,把一个有序整数数组放到二叉树中
- 二叉树的最小深度
- 给定一个有序数组,元素各不相同且升序排列,创建一个高度最小的二叉查找树
- 把顺序数组转为高度最小的二叉树(算法)
- 找出有序数组中绝对值最小的元素
- 求一个二叉树的最小深度
- 题目:二叉树的最小深度
- LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)
- 找出有序数组中绝对值最小的数
- 给定有序数组,创建高度最小的二叉查找树