标签:最大值 先序 语言 typedef 建二叉树 二分 创建 代码 print
题目:给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:
通过给定的数组构建最大二叉树,并且输出这个树的根节点。
Example 1:
输入: [3,2,1,6,0,5] 输入: 返回下面这棵树的根节点: 6 / 3 5 \ / 2 0 1
分析:(1)通过审题可知通过先序创建二叉树进行创建,即创建根节点,左子树,右子数。 所以选择递归的方法进行创建。
visit(T);
createleftTree(T->left);
creatterightTree(T->right);
(2)找数组中的最大值,类似于二分查找, 用三个游标low,high,temp分别来跟踪所要查找的数组中的低位、高位、和最大值的位置。
temp 根节点
[low , temp-1]左子树
[temp+1, high]右子数
(3)根据题意,输出时需要将二叉树按层输出,即按层遍历,使用队列实现,队列的特性FIFO 。(1)根节点入队列-》(2)根节点出队列并输出-》(3)左子树入队列-》(4)右子数入队列-》(3)
定义二叉树结点:
typedef struct TreeNode{
int val;
struct TreeNode *left;
struct TreeNode *right;
}TreeNode;
定义队列链表结点:
typedef struct QNode{
QueuePtr val;
QNode *next;
}QNode,*QueuePtr;
定义队列:
typedef struct Queue{
QueuePtr front;
QueuePtr rear;
}Queue;
//Q.front 指向队列链表的头结点;
Q.rear 指向队列链表的尾结点;
入队列时在Q.rear后加结点,出队列时在Q.front删结点
源代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
#include<stdio.h>
#include<stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
//定义链表队列
typedef struct QNode {
struct TreeNode *val;
struct QNode *next;
}QNode, *QueuePtr;
typedef struct Queue {
QueuePtr rear ; //队列尾指针
QueuePtr front; //队列头指针
}Queue;
// 找出数组中最大的数
int searchMaxnum(int *nums, int low, int high, int *location) {
int max = -1;
int i;
i = low;
while (i <=high) {
if (max< (*(nums + i)))
{
max = *(nums + i);
*location = i;
}
i++;
}
return max;
}
void createTree(struct TreeNode** p, int *count, int * nums, int low, int *location, int high) {
struct TreeNode* q;
int t,max, t_right, t_left;;
if (low > high)
{
return;
}
if (count == 0)
return;
max = searchMaxnum(nums, low, high, location);
if (max == -1)
return;
*p = q = (struct TreeNode*)malloc(sizeof(struct TreeNode));
(*p)->val = max;
(*p)->left = NULL;
(*p)->right = NULL;
(*count)--;
t_left = (*location) - 1;
t_right = (*location) + 1;
createTree(&((*p)->left), count, nums, low, location, t_left);
createTree(&((*p)->right), count, nums, t_right, location, high);
}
struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize) {
int maxleft, location,t, t_right, t_left;
int count = numsSize;
int low = 0;
int high = numsSize - 1;
struct TreeNode* p = NULL;
p = (struct TreeNode*)malloc(sizeof(struct TreeNode));
p->val = searchMaxnum(nums, low, high, &location);
count--;
(p)->left = NULL;
(p)->right = NULL;
t_left = location-1;
t_right = location + 1;
//createTree(&p, searchMaxnum(nums, low, high, &location), &count, nums, &low, &location, &high);
createTree(&(p->left), &count, nums, low, &location, t_left);
createTree(&(p->right), &count, nums, t_right, &location, high);
return p;
}
//入队列
void enqueue(Queue *s, struct TreeNode *t) {
QNode *p;
p = (QNode *)malloc(sizeof(QNode));
s->rear->next = p;
(s)->rear = p;
(s)->rear->val = t;
(s)->rear ->next= NULL;
}
void dequeue(Queue *s, struct TreeNode **t) {
QNode *p;
(*t) = (s)->front->next->val;
p = (s)->front;
(s)->front=(s)->front->next;
free(p);
}
void main() {
int num[8] = { 3,2,1,6,0,5,10,20};
struct TreeNode* p,*q,*t; //定义树节点
int count = 8;
Queue sq; //定义一个队列
t = NULL;
struct TreeNode* temp = NULL;
//创建最大二叉树
p = constructMaximumBinaryTree(num, count);
//printf("%d", p->val);
q = p;
//head=sq = NULL;
// sq->head = sq->rear;
//创建队列头结点,为空结点
sq.front = (QueuePtr)malloc(sizeof(QNode));
sq.rear = sq.front;
sq.rear->next = NULL;
//根节点入队列
if (p != NULL)
{
enqueue(&sq, p );
}
count = 8;
//按层遍历二叉树
while ((sq.front)!=(sq.rear)&& count!=0)
{
dequeue(&sq,&t);
if (t != temp)
{
printf("%d ", (t)->val);
if ((t)->left != NULL)
{
enqueue(&sq, (t)->left);//左孩子入队列
}
else
enqueue(&sq, temp);
if ((t)->right != NULL)
{
enqueue(&sq, (t)->right);
}
else
enqueue(&sq, temp);
count--;
}
else
printf("null ");
}
}
标签:最大值 先序 语言 typedef 建二叉树 二分 创建 代码 print
原文地址:https://www.cnblogs.com/feifiefighting/p/9069451.html