zoukankan      html  css  js  c++  java
  • [Leetcode 78] 105 Construct Binary Tree from Preorder and Inorder Traversal

    Problem:

    Given preorder and inorder traversal of a tree, construct the binary tree.

    Note:
    You may assume that duplicates do not exist in the tree.

    Analysis:

    We know that, for a binary tree, the structure of inorder traversal is as follows:

    |...left subtree inorder...| root |...right subtree inorder...|

    and the structure of preorder traversal is as follows:

    | root | ... left subtree preorder... | ... right subtree preorder... |

    So the root is always the first element of the given preorder traversal array. After that we must recursively deal with the two subtrees. So so probem becomes what's the inorder and preorder traversal of the tree's left- and right- subtree. By searching the index of the current root's val in the inorder traversal array, we can get the both subtree's length. Then we process it recursively.

    Notice the value we want is the length of each subtree, not the absolute index of each tree. So after getting the index of the root, we need to subtract the current i-start from the index.

    The exit condition is the range is not valid any more.

    Code:

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
    13         // Start typing your C/C++ solution below
    14         // DO NOT write int main() function
    15         return fun(inorder, 0, inorder.size()-1, 
    16                     preorder, 0, preorder.size()-1);
    17     }
    18     
    19 private:
    20     TreeNode* fun(vector<int> &in, int is, int ie,
    21                     vector<int> &pre, int ps, int pe) {
    22         if (is > ie || ps > pe)
    23             return NULL;
    24         
    25         TreeNode *t = new TreeNode(pre[ps]);                
    26         int idx = search(in, is, ie, pre[ps]);
    27         
    28         t->left = fun(in, is, idx-1, pre, ps+1, ps+idx-is);
    29         t->right = fun(in, idx+1, ie, pre, ps+idx-is+1, pe);
    30         
    31         return t;
    32     }
    33     
    34     int search(vector<int> &a, int s, int e, int v) {
    35         int idx;
    36         for (idx = s; idx <=e; idx++)
    37             if (a[idx] == v)
    38                 break;
    39                 
    40         return idx;
    41     }
    42 };
    View Code
  • 相关阅读:
    python2代码改成python3踩过的坑
    Mac下为什么有的文件名后带一个* 星号?
    Mac 的 Vim 中 delete 键失效的原因和解决方案(转)
    使用pandas处理大型CSV文件(转)
    Java基础——02
    javaee相关基础
    Cookie&Session笔记
    EL&JSTL笔记------jsp
    JavaWeb基础
    Java基础——01
  • 原文地址:https://www.cnblogs.com/freeneng/p/3207824.html
Copyright © 2011-2022 走看看