zoukankan      html  css  js  c++  java
  • 105. 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.

    For example, given

    preorder = [3,9,20,15,7]
    inorder = [9,3,15,20,7]

    Return the following binary tree:

        3
       / 
      9  20
        /  
       15   7
    此问题的对应的中文描述是根据前序和中序得到树状结构体。
    为了解决这个问题,我们是否可以根据两个数组得到左孩子和右孩子。通过观察得到一下性质:在前序数组里的第一个一定为根结点,即3一定为根结点。根据3为根结点,以及中序数组,我们可以判断
    9为左孩子,[15,20,7]为右孩子。抽象出规律,即对于中序数组,在3右边的为左孩子,在3右边的为右孩子。
    至此,我们已经可以根据这两个数组判断出根,左孩子,右孩子。

    根据分而治之的思想,以及递归的程序实现方式,可以分成如下流程。

    1、找出根结点
    2、分离出左树和右树
    3、递归的对左树和右树进行转化
    4、将左树和右树连接到根结点
    5、返回

    再分有无左右子树进行细节化。

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     struct TreeNode *left;
     6  *     struct TreeNode *right;
     7  * };
     8  */
     9 struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int inorderSize) {
    10     
    11     
    12     int index;
    13     struct TressNode *left=NULL,*right=NULL;
    14     struct TreeNode *node;
    15     
    16     node = malloc(sizeof(struct TreeNode));
    17     node->val = preorder[0];
    18     
    19     //ERROR input check.
    20     if(preorderSize==0) {
    21         return NULL;
    22     }
    23     
    24     //base condition
    25     if(preorderSize==1){
    26         node->left = NULL;
    27         node->right = NULL;
    28         return node;
    29     }
    30     
    31     for (int i=0; i<preorderSize; i++) {
    32         if(inorder[i]==preorder[0]) { index=i; break;}
    33     }
    34     //this node have left and right children.
    35     if(index>0 && index<inorderSize-1) {
    36         left = buildTree(&preorder[1], index, inorder, index);
    37         right = buildTree(&preorder[index+1], preorderSize-1-index, &inorder[index+1], inorderSize-1-index);
    38     }
    39     //this node have only right children.
    40     if(index==0 && index<inorderSize-1 ){
    41         left = NULL;
    42         right = buildTree(&preorder[index+1], preorderSize-1-index, &inorder[index+1], inorderSize-1-index);   
    43     }
    44     //this node have only left children.
    45     if(index>0 && index==inorderSize-1){
    46         left = buildTree(&preorder[1], index, inorder, index);
    47         right = NULL;
    48     }
    49     
    50     node->left = left;
    51     node->right = right;
    52     return node;
    53 }
  • 相关阅读:
    1442. Count Triplets That Can Form Two Arrays of Equal XOR
    1441. Build an Array With Stack Operations
    312. Burst Balloons
    367. Valid Perfect Square
    307. Range Sum Query
    1232. Check If It Is a Straight Line
    993. Cousins in Binary Tree
    1436. Destination City
    476. Number Complement
    383. Ransom Note
  • 原文地址:https://www.cnblogs.com/midhillzhou/p/8657261.html
Copyright © 2011-2022 走看看