LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal

一:LeetCode 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.

链接:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

分析:此题就是由二叉树的前序和中序遍历的结果构建这颗二叉树

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void createTree(TreeNode *p, vector<int> &preorder, int pstart, int pend, vector<int> &inorder, int istart, int iend, int flag){
        if(pstart > pend) return;
        p = new TreeNode(preorder[pstart]);
        if(root == NULL) root = pNode = p;
        else{
            if(flag) pNode->right = p;     //  主要是将这颗树连接起来
            else pNode->left = p;
            pNode = p;
        }
        if(pstart == pend) return;
        int i = istart;
        for(; i <= iend; i++){
            if(inorder[i] == preorder[pstart])break;
        }
        int k = i - istart;    // 左子树有多少个元素
        createTree(p->left, preorder, pstart+1, pstart+k, inorder, istart, i-1, 0);   // 创建左子树 flag=0表示创建左子树
        pNode = p;
        createTree(p->right, preorder, pstart+k+1, pend, inorder, i+1, iend, 1);     // 创建右子树 flag=1表示创建右子树
    }

    TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
        root = NULL;
        pNode = NULL;
        int n = preorder.size();
        if(n == 0) return root;
        createTree(root, preorder, 0, n-1, inorder, 0, n-1, 0);
        return root;

    }
private:
    TreeNode *root, *pNode;
};

二:LeetCode106 Construct
Binary Tree from Inorder and Postorder Traversal

题目:

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

Note:

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

链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/

分析:此题是由中序和后序遍历的结果构建二叉树

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void createTree(TreeNode *p, vector<int> &inorder, int istart, int iend, vector<int> &postorder, int pstart, int pend, int flag){
        if(pstart > pend) return;
        p = new TreeNode(postorder[pend]);
        if(root == NULL) root = pNode = p;
        else{
            if(flag) pNode->right = p;    //  将这颗树连接起来
            else pNode->left = p;
            pNode = p;
        }
        if(pstart == pend) return;
        int i = istart;
        for(; i<= iend; i++){
            if(inorder[i] == postorder[pend]) break;
        }
        int k = i - istart;
        createTree(p->left, inorder, istart, i-1, postorder, pstart, pstart+k-1, 0);   // 创建左子树
        pNode = p;
        createTree(p->right, inorder, i+1, iend, postorder, pstart+k, pend-1, 1);   // 创建右子树

    }
    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
        root = pNode = NULL;
        int n = inorder.size();
        if(n == 0) return root;
        createTree(root, inorder, 0, n-1, postorder, 0, n-1, 0);
        return root;

    }
private:
    TreeNode *root, *pNode;

};
时间: 2024-10-06 18:45:35

LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal的相关文章

[Leetcode][Tree][Construct Binary Tree from Preorder/Postorder and Inorder Traversal ]

从树的中序遍历+前/后序遍历重建一棵树. 必须使用iterator才能过,否则会MLE. 1.preorder + inorder 第一个版本,使用坐标范围: 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) {}

LeetCode 105 106:Construct Binary Tree from Preorder/Postorder and Inorder Traversal

LeetCode 105: Given preorder and inorder traversal of a tree, construct the binary tree. 给定一个二叉树的前序和中序遍历,重建这棵二叉树. LeetCode 106: Given inorder and postorder traversal of a tree, construct the binary tree. 给定一个二叉树的后序和中序遍历,重建这棵二叉树. 两题含义差不多,我们先从105看起,105

【LeetCode】105 &amp; 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree. Input: 105. Preorder & Inorder traversal 106. Inorder & Postorder traversal output: A binar

leetcode || 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. Hide Tags Tree Array Depth-first Search 题意:利用二叉树的前序遍历序列和中序遍历序列 构造二叉树,很经典的一道题 thinking: (1)前序遍历的顺序是:父节点-

【LeetCode】105 &amp; 106. Construct Binary Tree from Inorder and Postorder Traversal

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 提示: 题目要求通过一颗二叉树的中序遍历及后续遍历的结果,将这颗二叉树构建出来,另外还有一个已知条件,所有节点的值都是不同的. 首先需要了解一下二叉树不同遍历方式的定义: 前序遍历:首先访问根结点,然后遍历左子树,最

[leedcode 106] 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. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right;

【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal-通过中序和后续遍历还原二叉树

一.描述: 二.思路: 二叉树的中序遍历和前序遍历或和后续遍历能唯一确定一节课二叉树,即2中还原方式都需要中序遍历才能完成: 设二叉树的前序遍历序列为{1, 2, 4, 5, 3, 6},中序遍历序列为{4,2,5,1, 3, 6}:(红色标记表示以还原节点!!!) (1)-前序遍历的第一个节点是二叉树的根节点,{1, 2, 4, 5, 3, 6},对应中序中的位置是{4,2,5,1, 3, 6},所以中序序列中的 '1' 之前的全部元素为左子树元素,'1'之后的为右子树元素: (2)-左子树对

[LeetCode]*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. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not