hdu3999-The order of a Tree (二叉树的先序遍历)

http://acm.hdu.edu.cn/showproblem.php?pid=3999

The order of a Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1361    Accepted Submission(s): 695

Problem Description

As we know,the shape of a binary search tree is greatly related to the order of keys we insert. To be precisely:
1.  insert a key k to a empty tree, then the tree become a tree with
only one node;
2.  insert a key k to a nonempty tree, if k is less than the root ,insert
it to the left sub-tree;else insert k to the right sub-tree.
We call the order of keys we insert “the order of a tree”,your task is,given a oder of a tree, find the order of a tree with the least lexicographic order that generate the same tree.Two trees are the same if and only if they have the same shape.

Input

There are multiple test cases in an input file. The first line of each testcase is an integer n(n <= 100,000),represent the number of nodes.The second line has n intergers,k1 to kn,represent the order of a tree.To make if more simple, k1 to kn is a sequence of 1 to n.

Output

One line with n intergers, which are the order of a tree that generate the same tree with the least lexicographic.

Sample Input

4

1 3 4 2

Sample Output

1 3 2 4

题解:题目意思即,给你一个插入数列,形成一棵二叉树,你给出字典序最小的插入方法建相同的一棵树出来。说白了,就是求先序序列。

代码:

 1 #include <fstream>
 2 #include <iostream>
 3
 4 using namespace std;
 5
 6 typedef struct Node{
 7     Node *lch,*rch,*nex;
 8     int x;
 9     Node(int x){
10         this->x=x;
11         lch=NULL;
12         rch=NULL;
13     }
14 }inode;
15
16 int n,tn;
17 inode *head;
18
19 void insert(int t);
20 void preOrder(inode *p);
21
22 int main()
23 {
24     //freopen("D:\\input.in","r",stdin);
25     //freopen("D:\\output.out","w",stdout);
26     int t;
27     while(~scanf("%d",&n)){
28         scanf("%d",&t);
29         head=new inode(t);
30         for(int i=1;i<n;i++){
31             scanf("%d",&t);
32             insert(t);
33         }
34         tn=0;
35         preOrder(head);
36     }
37     return 0;
38 }
39 void insert(int t){
40     inode *p=head,*s=new inode(t);
41     while(p!=NULL){
42         if(t<p->x)
43             if(p->lch!=NULL)    p=p->lch;
44             else{
45                 p->lch=s;
46                 break;
47             }
48         else
49             if(p->rch!=NULL)    p=p->rch;
50             else{
51                 p->rch=s;
52                 break;
53             }
54     }
55 }
56 void preOrder(inode *p){
57     if(p!=NULL){
58         printf("%d",p->x);
59         if(++tn<n)  printf(" ");
60         else    printf("\n");
61         preOrder(p->lch);
62         preOrder(p->rch);
63         delete p;
64     }
65 }
时间: 2025-01-06 01:15:31

hdu3999-The order of a Tree (二叉树的先序遍历)的相关文章

hdu3999 The order of a Tree

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3999 题意:给一序列,按该序列插入二叉树,给出字典序最小的插入方法建相同的一棵树出来.即求二叉树的先序遍历. #include<bits/stdc++.h> using namespace std; struct node { int v; node *left,*right; }*root; node* build(node *root,int v) { if(root==NULL) { roo

[LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means? > read

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

[LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 经典题目,求二叉树的后序遍历的非递归方法,跟前序,中序,层序一样都需要用到栈,后续的顺序

lintcode 容易题:Binary Tree Inorder Traversal 二叉树的中序遍历

题目: 二叉树的中序遍历 给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 解题: 程序直接来源 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val

leetcode 题解:Binary Tree Inorder Traversal (二叉树的中序遍历)

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterative

Binary Tree Postorder Traversal 二叉树的后序遍历

地址:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题意就是完成二叉树的后序遍历,我们知道如果使用递归进行二叉树后序遍历将是非常简单的事情. public class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer > ans = new ArrayList<>(); Tr

Java数据结构系列之——树(4):二叉树的中序遍历的递归与非递归实现

package tree.binarytree; import java.util.Stack; /** * 二叉树的中序遍历:递归与非递归实现 * * @author wl * */ public class BiTreeInOrder { // 中序遍历的递归实现 public static void biTreeInOrderByRecursion(BiTreeNode root) { if (root == null) { return; } biTreeInOrderByRecursi

LintCode 二叉树的中序遍历

给出一棵二叉树,返回其中序遍历 样例 给出二叉树 {1,#,2,3}, 1 2 / 3 返回 [1,3,2]. 挑战 你能使用非递归算法来实现么? 分析:同前序遍历. /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->righ

SDUT 1489 求二叉树的先序遍历 (中序后序还原二叉树)

求二叉树的先序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据.每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的中序遍历序列,第二个字符串表示二叉树的后序遍历序列. Output 输出二叉树的先序遍历序列 Exampl