SDUT 2128 树结构练习——排序二叉树的中序遍历
2016-07-23 11:08
330 查看
点击打开题目链接
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *left, *right;
};
int k;
int _array[1010];
void Inorder(node *root);
void Insert(node *(&root), int x);
node *creat(int lenth, int _array[]);
int main()
{
node *root;
int n;
while(cin >> n)
{
k = 0;
for(int i = 0; i < n; i++)
{
cin >> _array[i];
}
root = creat(n, _array);
Inorder(root);
for(int i = 0; i < k; i++)
{
if(i == k-1)
{
cout << _array[i] << endl;
}
else
{
cout << _array[i] << ' ';
}
}
}
return 0;
}
node *creat(int lenth, int _string[])
{
node *root = NULL;
int i = 0;
while(i < lenth)
{
Insert(root, _string[i]);
i++;
}
return root;
}
void Insert(node *(&root), int x)
{
if(!root)
{
root = new node;
root -> data = x;
root -> left = NULL;
root -> right = NULL;
}
else if(x < root -> data)
{
Insert(root -> left, x);
}
else
{
Insert(root -> right, x);
}
}
void Inorder(node *root)
{
if(root)
{
Inorder(root -> left);
_array[k++] = root -> data;
Inorder(root -> right);
}
}
#include <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *left, *right;
};
int k;
int _array[1010];
void Inorder(node *root);
void Insert(node *(&root), int x);
node *creat(int lenth, int _array[]);
int main()
{
node *root;
int n;
while(cin >> n)
{
k = 0;
for(int i = 0; i < n; i++)
{
cin >> _array[i];
}
root = creat(n, _array);
Inorder(root);
for(int i = 0; i < k; i++)
{
if(i == k-1)
{
cout << _array[i] << endl;
}
else
{
cout << _array[i] << ' ';
}
}
}
return 0;
}
node *creat(int lenth, int _string[])
{
node *root = NULL;
int i = 0;
while(i < lenth)
{
Insert(root, _string[i]);
i++;
}
return root;
}
void Insert(node *(&root), int x)
{
if(!root)
{
root = new node;
root -> data = x;
root -> left = NULL;
root -> right = NULL;
}
else if(x < root -> data)
{
Insert(root -> left, x);
}
else
{
Insert(root -> right, x);
}
}
void Inorder(node *root)
{
if(root)
{
Inorder(root -> left);
_array[k++] = root -> data;
Inorder(root -> right);
}
}
相关文章推荐
- MIT Introduction to Algorithms 学习笔记(六)
- Binary Search Tree Iterator,Reverse Nodes in k-Group
- Find Peak Element-找出峰值元素w问题描述
- Search in Rotated Sorted Array-旋转数组的查找
- [LeetCode] Convert Sorted List to Binary Search Tree
- [LeetCode] Convert Sorted Array to Binary Search Tree
- [LeetCode] Recover Binary Search Tree
- [LeetCode] Validate Binary Search Tree
- [LeetCode] Unique Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- LeetCode:Binary Search Tree相关题目合集
- LeetCode-Convert Sorted Array to Binary Search Tree
- Convert Sorted Array(List) to Binary Search Tree
- Validate Binary Search Tree
- convert sorted array to binary search tree
- Binary Tree Inorder Traversal
- Binary Search Tree Iterator
- Binary Tree Postorder Traversal
- Kth Smallest Element in a BST
- contains duplicate 3