Ultraviolet A 548 (Binary Tree), uva548 Binary Tree

Source: Internet
Author: User

Ultraviolet A 548 (Binary Tree), uva548 Binary Tree

Question: This section describes the central and post order of a binary tree, re-constructs the binary tree, and outputs the path and the minimum leaf value.

Two templates:

Returns the result of the forward and middle order:

Node* build (int n, int* preo, int* ino) {    Node* node = new Node;    int i = 0;    if (n <= 0)        return NULL;    while (ino[i] != preo[0])        i++;    node -> val = ino[i];    node -> left = build(i, preo + 1, ino);    node -> right = build(n - i - 1, preo + i + 1, ino + i + 1);    return node;}

Returns the construction of the middle and back orders:

Node* build (int n, int* ino, int* post) {    Node* node = new Node;    int i = n - 1;    if (n <= 0)        return NULL;    while (ino[i] != post[n - 1])        i--;    node -> val = ino[i];    node -> left = build(i, ino, post);    node -> right = build(n - i - 1, ino + i + 1, post + i);    return node;}

After the binary tree is rebuilt, the dfs traverses to find the minimum path and leaves.

#include <stdio.h>const int N = 10010;const int INF = 0x3f3f3f3f;struct Node {    int val;    Node* left;    Node* right;    Node () {        val = 0;        left = right = 0;    }};int min, ans;void init() {    min = INF;}Node* build (int n, int* ino, int* post) {    Node* node = new Node;    int i = n - 1;    if (n <= 0)        return NULL;    while (ino[i] != post[n - 1])        i--;    node -> val = ino[i];    node -> left = build(i, ino, post);    node -> right = build(n - i - 1, ino + i + 1, post + i);    return node;}void dfs (Node* node, int sum) {    if (node == NULL)        return;    sum += node -> val;    if (node -> left == NULL && node -> right == NULL) {        if (sum < min) {            ans = node -> val;            min = sum;        }    }    else {        if (node -> left != NULL)            dfs(node -> left, sum);        if (node -> right != NULL)            dfs(node -> right, sum);    }}void Delete(Node* node) {    if (node == NULL)        return;    if (node -> left != NULL)        Delete(node -> left);    if (node -> right != NULL)        Delete(node -> right);    delete node;}int main() {    int a, n = 0, post[N], ino[N];    Node* root;    char c;    while (scanf("%d%c", &ino[n++], &c) != EOF) {        if (c == '\n') {            n = 0;            while (scanf("%d%c", &post[n++], &c)) {                if (c == '\n') {                    init();                    root = build(n, ino, post);                    dfs(root, 0);                    printf("%d\n", ans);                    Delete(root);                    n = 0;                    break;                }           }        }    }    return 0;}



What's wrong with the WordPress 548-Tree?

The AC code. In the question, we will give you a binary tree's central and backward traversal.
The nodes here are in the range of 1 to 10000, with a maximum of 10000 nodes. However, the nodes here are not consecutive, not that there are N nodes that are numbered from 1 to N.
For example
1 7 8
1, 8, and 7.
Below is my AC code
# Include <cstdlib>
# Include <stdio. h>
# Include <string. h>
Const int MAX = 10005;
Int in [MAX];
Int post [MAX];
Int son [MAX] [2];
Char s [1000000];
Int p [MAX];
Int ind, dis [MAX];
Int deal (int x [], char s [])
{
Int n = 0, I;
For (I = 0; s [I]; I ++)
{
If (s [I]> = '0' & s [I] <= '9 ')
{
X [n] = 0;
While (s [I] & s [I]> = '0' & s [I] <= '9 ')
{
X [n] = x [n] * 10 + s [I]-'0 ';
I ++;
}
I --;
N ++;
}
}
Return n;
}
Int DFS (int low, int high)
{
If (low> = high) return-1;
If (ind =-1) return-1;
Int r = post [ind];
Int rp = p [r];
If (rp> = low & rp {
Ind --;

Son [r] [1] = DFS (rp + 1, high );
Son [r] [0] = DFS (low, rp );
Return r;
}
Else return-1;
}
Void getDis (int r)
{
Int t;
If (son [r] [0]! =-1)
{
T = son [r] [0];
Dis [t] = dis [r] + t;
GetDis (t );
}
If (son [r] [1]! =-1)
{
T = son [r] [1];
Dis [t] = dis [r] + t;
GetDis (t );
}
}
Void inorder (int r)
{
If (son [r] [0]! =-1) inorder (son [r] [0]);
Printf ("% d", r );
If (son [r] [1]! =-1) inorder (son [r] [1]);
}
Void postorder (int r)
{
If (son [r] [0]! =-1) postorder (son [r] [0]);

If (son [r] [1]! =-1) postorder (son [r] [1]);
Printf ("% d", r );
}
Int main ()
{
Int n, I, k;
While (gets (s ))
{
N = deal (in, s );
Gets (s );
N = deal (post, s );
If (n = 1)
{
Printf ("% d \ n", in [0]);
Continue;
}
Memset (son, 0, sizeof (son ));
For (I = 0; I <n; I ++) p [in [I] = I;
Ind = n-2;

... The remaining full text>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.