【leetcode】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.

 1 class Solution {
 2 public:
 3     TreeNode* createTree(vector<int>& inorder,int instart,int inend,vector<int> &postorder,int poststart,int postend)
 4     {
 5         if(instart>inend) return NULL;
 6
 7         int root=postorder[postend];
 8
 9         int index;
10
11         for(int i=instart;i<=inend;i++)
12         {
13             if(inorder[i]==root)
14             {
15                 index=i;
16                 break;
17             }
18         }
19
20         int len=index-instart;
21         TreeNode *left=createTree(inorder,instart,index-1,postorder,poststart,poststart+len-1);
22         TreeNode *right=createTree(inorder,index+1,inend,postorder,poststart+len,postend-1);
23
24         TreeNode *node=new TreeNode(root);
25         node->left=left;
26         node->right=right;
27
28         return node;
29     }
30     TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
31
32         if(inorder.size()==0) return NULL;
33
34         TreeNode *head=createTree(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1);
35         return head;
36     }
37 };
时间: 2024-10-15 12:03:41

【leetcode】Construct Binary Tree from Inorder and Postorder Traversal的相关文章

【LeetCode】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. [解析] 题意:根据二叉树中序遍历和后序遍历的结果,构造该二叉树. 首先明确一下,中序遍历顺序:left - root - right,后序遍历顺序:left  - right - root. 很显然,后序遍历的

【Leetcode】【Medium】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. 解题思路: 给出一个二叉树的中序和后序遍历结果,还原这个二叉树. 对于一个二叉树: 1 / 2 3 / \ / 4 5 6 7 后序遍历结果为:4 5 2 6 7 3 1 中序遍历结果为:4 2 5 1 6 3 7 由此可以

【Leetcode长征系列】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. 思路:和上一题一样,后续我们可以通过最后一个值得到根的值,同样可以通过定位根的值得到左右子树的子集,递归求解即可. 代码: /** * Definition for binary tree * struct Tre

LeetCode | 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树【Python】

LeetCode 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树[Medium][Python][二叉树][递归] Problem LeetCode Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  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. 代码: class Solution { public: TreeNode *buildTr

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

Leetcode dfs Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 14363 Total Submissions: 54254My Submissions Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in t

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: 115870 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp;amp; Construct Binary Tree f

1.  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. 代码: class Solution { public: TreeNode *buildTr