Construct Binary Tree from Inorder and Postorder Traversal

OJ: https://oj.leetcode.com/problems/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 TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
TreeNode *createTree(vector<int> &inorder, int start, int end, vector<int> &postorder, int start2, int end2) {
if(start > end || start2 > end2) return NULL;
TreeNode *root = new TreeNode(postorder[end2]);
int i;
for(i = start; i <= end; ++i)
if(inorder[i] == postorder[end2]) break;
// if(i > end) throws std::exception("error");
root->left = createTree(inorder, start, i-1, postorder, start2, start2 + i-start-1);
root->right = createTree(inorder, i+1, end, postorder, start2+i-start, end2-1);
return root;
} class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return createTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
}
};

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 binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
TreeNode* createTree(vector<int> &preorder, int start, int end, vector<int> &inorder, int start2, int end2) {
if(start > end || start2 > end2) return NULL;
TreeNode *root = new TreeNode(preorder[start]);
int i;
for(i = start2; i <= end2; ++i)
if(preorder[start] == inorder[i]) break;
root->left = createTree(preorder, start+1, start+i-start2, inorder, start2, i-1);
root->right = createTree(preorder, start+i-start2+1, end, inorder, i+1, end2);
return root;
}
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return createTree(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1);
}
};

36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. 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 ...

  2. 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...

  3. leetcode-1006 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 ...

  4. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  5. [LeetCode-21]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 ...

  6. 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】

    我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列,           ======O=========== 后序遍 ...

  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 tha ...

  8. LeetCode OJ 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 ...

  9. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. NSURLCache 和 NSCache 的区别

    NSURLCache 和 NSCache 的区别 NSURLCache提供的是URL Request缓存,可以在Memory和Disk上:NSCache提供了HTTP Request外的东西的缓存方式 ...

  2. python3读取chrome浏览器cookies

    好几年前我在做一些自动化的脚本时,脑子里也闪过这样的想法:能不能直接把浏览器的cookies取出来用呢? 直到昨天看到代码<python模拟发送动弹>,想起来当年我也曾经有类似的想法没能完 ...

  3. Android平板电脑开发— — —碎片

    碎片是一种可以嵌入在活动中的UI片段,它能让程序更加合理与充分地使用大屏幕的空间,碎片通常都是在平板电脑开发中才会使用 简单实例 左碎片布局 <?xml version="1.0&qu ...

  4. 如何取消win10电脑自动更新

    windows 10系统中关闭windows自动更新步骤如下:1.按键盘上的“Windows徽标键+R”组合键,可以调出“运行”窗口. 2.输入gpedit.msc,单击“确定”,可以打开“本地组策略 ...

  5. 从零开始学习Node.js例子一 http get和post

    httpserverrequestget.js /* 获取GET请求内容 由于GET请求直接被嵌入在路径中,URL是完整的请求路径,包括了?后面的部分,因此你可以手动解析后面的内容作为GET请求的参数 ...

  6. HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. 第二个Sprint冲刺团队贡献分

    201306114322 邵家文 50分 201306114319 陈俊金 10分 201306114320 李新    10分 201306114324 朱浩龙 10分

  8. PAT (Basic Level) Practise:1030. 完美数列

    [题目链接] 给定一个正整数数列,和正整数p,设这个数列中的最大值是M,最小值是m,如果M <= m * p,则称这个数列是完美数列. 现在给定参数p和一些正整数,请你从中选择尽可能多的数构成一 ...

  9. fastqc, Per Base Sequence Content

    Per Base Sequence Content对所有reads的每一个位置,统计ATCG四种碱基(正常情况)的分布: 横轴为位置,纵轴为百分比. 正常情况下四种碱基的出现频率应该是接近的,而且没有 ...

  10. hdu 4336 Card Collector

    dp+状态压缩 #include<cstdio> using namespace std; ]; <<]; int main() { int n; while(scanf(&q ...