题目:1385 由前序和中序构建二叉树

http://ac.jobdu.com/problem.php?pid=1385

蛮怀旧的题目,记得大一就见过一直没做过,没难度,纯小心吧。

类似的是有中序和后续构建二叉树。比如http://www.cnblogs.com/kaituorensheng/p/3788533.html

思路很简单 递归构造:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

const int SIZE = 1000+10;
struct Node{
    int v;
    Node *left;
    Node *right;
    Node(){
        v = -1;
        left = right = NULL;
    }
    Node(int val, Node *l, Node *r){
        v = val;
        left = l;
        right = r;
    }
};

int n;
int head[SIZE],mid[SIZE];
int flag,getNode;

Node* buildTree(int headptr, int midleft, int midright){
    /*cout << "*********" << endl;
    cout << "head=" << head[headptr] << endl;
    cout << "midleft=" << midleft << endl;
    cout << "midright=" << midright << endl;*/

    Node *father = new Node();
    int pos=-1;
    if(midleft == midright){
        if(head[headptr] == mid[midleft]){
            father->left = father->right = NULL;
            father->v = head[headptr];
            getNode++;
            return father;
        }else{
            return NULL;
        }
    }
    if(midleft > midright)return NULL;
    for(int i=midleft;i<=midright;i++){
        if(head[headptr] == mid[i])
            pos = i;
    }
    if(pos == -1)return NULL;
    int hasleft=0;//,hasright=0;
    if(pos == midleft){//意味着左子树为空
        father->left = NULL;
    }else{
        hasleft = 1;
        father->left = buildTree( headptr+1 , midleft, pos-1);
        if(father->left == NULL)return NULL;
    }
    if(pos == midright){
        father->right = NULL;
    }else{
        if(hasleft == 1){
            //左子树结点个数 pos-midleft
            father->right = buildTree(headptr+pos-midleft+1, pos+1, midright);
        }else{
            father->right = buildTree(headptr+1, pos+1, midright);
        }
        if(father->right == NULL)return NULL;
    }
    father->v = head[headptr];

    getNode++;
    return father;
}

/*void print(Node *nod){
    if(flag)putchar(' ');
    else flag=1;
    printf("%d", nod->v);
}*/

void print(Node *nod){
    //if(flag)putchar(' ');
    //else flag=1;
    printf("%d ", nod->v);
}

void sear(Node *father){
    //if(father->left == NULL && father->right == NULL){//叶子
        //print(father);
    //}
    if(father->left)sear(father->left);
    if(father->right)sear(father->right);
    print(father);
}

int main(){
    //freopen("06.txt", "r", stdin);
    int in;
    while(~scanf("%d", &n)){
        flag = getNode = 0;
        for(int i=0;i<n;i++){
            scanf("%d", &in);
            head[i] = in;
        }
        for(int i=0;i<n;i++){
            scanf("%d", &in);
            mid[i] = in;
        }
        Node *ancestor = buildTree(0, 0, n-1);
        if(ancestor == NULL || getNode != n){
        //if(ancestor == NULL ){
            printf("No\n");
        }else{
            sear(ancestor);
            printf("\n");
        }
        //cout << getNode << endl;

    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-01 02:41:50

题目:1385 由前序和中序构建二叉树的相关文章

Leetcode:Construct Binary Tree 前序和中序、后序和中序构建二叉树

前序和中序构建二叉树 后序和中序构建二叉树 分析:主要思路就是 在中序中找根节点然后划分左右子树,具体如下: 1. 查找根节点. 我们知道前序序列的第一个元素 和 后序序列的最后一个元素 肯定是根节点,我们就以此为突破口 2. 确定根节点的坐标. 我们在 中序序列中找到 根节点 的下标. 3. 分割左右子树. 对于中序序列:根节点下标之前的都属于左子树,之后的都属于右子树 对于先序序列:根节点之后的 一段元素(该段长度可以由中序序列的左子树长度确定)属于左子树,左子树之后的元素属于右子树 对于先

根据前序和中序重建二叉树

注意:1.仅根据前序和后序无法构建唯一的二叉树:2.二叉树前序遍历,第一个数字总是树的根节点的值:3.中序遍历中,根节点的值在序列的中间,左子树的值子在根节点的值的左边,右字树的值在根节点的值的右边:4.思路:递归 #include <iostream> #include <stdlib.h> using namespace std; struct Node{ int value; Node* left; Node* right; }; Node* ConstructCore(in

前序和中序构造二叉树

题目链接: 涉及知识:二叉树的遍历 分析: 二叉树的前序遍历:根节点 -> 左子树 -> 右子树 二叉树的中序遍历:左子树 -> 根节点 -> 右子树 由此可知:前序遍历中访问到的第一个元素便是根节点,通过该点便可以将中序遍历分成左右两部分,左部分的元素用来生成该二叉树的左子树,右部分用来生成二叉树的右子树. 同样,左右两部分的元素中,首先在前序遍历中出现的便是该子树的根节点,很明显符合递归的定义. 代码如下: /* * @lc app=leetcode.cn id=105 lan

根据先序和中序构建二叉树(java)后序输出

先序序列:   1,2,4,8,5,3,6,7 中序序列:   8,4,2,5,1,6,3,7 //节点类 /** * */ package Tree; /** * @author 邢兵 * @data * @description */ public class Node { public Object data; public Node left; public Node right; //创建一个空节点 public Node(){ this(null); } //创建一个没有孩子的节点

前序中序构建二叉树

public Node PreMidToTree(int[] pre,int[] mid) { if (pre == null || mid == null) return; Dictionary<int, int> dic = new Dictionary<int, int>(); for (int i = 0; i < mid.Length; i++) dic[mid[i]] = i; return PreMid(pre, 0, pre.Length - 1, mid,

C++根据前序和中序构造二叉树

#include <iostream> #include <string.h> using namespace std; template<typename Type> struct Node { Type data; Node<Type> *left; Node<Type> *right; Node(Type d = Type()):data(d),left(NULL),right(NULL){} }; template<typename

construct-binary-tree-from-preorder-and-inorder-traversal——前序和中序求二叉树

Given preorder and inorder traversal of a tree, construct the binary tree. Note:  You may assume that duplicates do not exist in the tree. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right

(树)根据中序后序构建二叉树

题目:根据中序和后序遍历构建二叉树 思路:利用递归加上分治的思想.先找到根节点的值,然后在根据中序遍历找到根节点的左右两边的值,然后在递归的处理左右两边的左右子树.这里的关键在于怎么处理递归的左右子树的范围,代码里面详细解释 代码: class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { vector<int>::size_t

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(