把一个有序整数数组放到二叉树
来源:互联网 发布:云计算前景200字分析 编辑:程序博客网 时间:2024/05/18 03:05
对于有序数组,取中间节点作为根节点,将数组分成两部分,对数组的两部分递归构建左右子树,代码如下:
#include <iostream>using namespace std;struct TreeNode {int m_pData;TreeNode* m_pLeft;TreeNode* m_pRight;};void CreateTree(int *p, int len, TreeNode* &pHead){if(len > 0){pHead = new TreeNode;int mid = len / 2;pHead->m_pData = p[mid];pHead->m_pLeft = NULL;pHead->m_pRight = NULL;CreateTree(p,mid,pHead->m_pLeft);CreateTree(p+mid+1,len-mid-1,pHead->m_pRight);}else{pHead = NULL;}}void InOrder(TreeNode *pHead){if(pHead != NULL){InOrder(pHead->m_pLeft);cout<<pHead->m_pData<<" ";InOrder(pHead->m_pRight);}}int main(){int arr[] = {1,2,3,4,5,6,7,8,9,10};TreeNode *pHead = NULL;CreateTree(arr,sizeof(arr)/sizeof(arr[0]),pHead);InOrder(pHead);cout<<endl;return 0;}
0 0
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 题目:把一个有序整数数组放到二叉树。
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树
- 把一个有序整数数组放到二叉树。
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中?
- 怎样编写一个程序,把一个有序整数数组放到二叉树中(按行输出)
- 编写一个程序,把一个有序整数数组放到二叉树中。
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 5、怎样编写一个程序,把一个有序整数数组放到二叉树中
- 编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 2015校招笔试面试算法总结之蓝汛笔试
- LeetCode:Divide Two Integers
- HDU 1002
- js 动态合并单元格
- 设计模式(二)The Observer Pattern 观察者模式
- 把一个有序整数数组放到二叉树
- 二叉树的三种非递归遍历
- 的广泛地
- 注解入门
- MFC执行顺序
- python使用PIL处理图片的一些收获和经验
- 当时德国
- 二叉树所有根到叶子节点路径打印
- 安卓开发中Android消息机制详解