Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 845 Accepted Submission(s): 461
node
#include <stdio.h> #include <string.h> #include <iostream> #include <stack> using namespace std; typedef struct node { int data; node *lchild; node *rchild; node() { lchild = rchild = NULL; } }TreeNode; void CreateTree(TreeNode *&pRoot, int data) { if (pRoot == NULL) { pRoot = new TreeNode; pRoot->data = data; } else { if (data > pRoot->data) { CreateTree(pRoot->rchild, data); } else { CreateTree(pRoot->lchild, data); } } } void PreOrder(TreeNode *pRoot) { int nCount = 0; if (pRoot == NULL) { return; } stack<TreeNode*> Stack; Stack.push(pRoot); do { TreeNode *p = Stack.top(); Stack.pop(); if (nCount == 0) { printf("%d", p->data); nCount++; } else { printf(" %d", p->data); nCount++; } if (p->rchild != NULL) { Stack.push(p->rchild); } if (p->lchild != NULL) { Stack.push(p->lchild); } } while (!Stack.empty()); } int main() { int n, num; scanf("%d", &n); TreeNode *pRoot = NULL; for (int i = 0; i < n; i++) { scanf("%d", &num); CreateTree(pRoot, num); } PreOrder(pRoot); printf("\n"); return 0; }