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. My Game --线段数据

    在背景中用到了一个自定义的类 VectArr : class VectArr { public: VectArr( const Bezier & bz, int conut = 30 ) : ...

  2. Android--JUnit单元测试

      Android--JUnit单元测试 前言 本篇博客说明一下在Android开发中,如何使用JUnit进行单元测试.首先来了解一下什么是JUnit,JUnit测试是白盒测试,即主要是程序员自己对开 ...

  3. 在可以调用OLE之前,必须将当前线程设置为单线程单元(STA)模式,请确保您的Main函数带有STAThreadAttribute

    导入导出功能,在调用ShowDialog时的错误,解决办法如下: WinForm窗体的入口点: /// <summary> /// 应用程序的主入口点. /// </summary& ...

  4. 下载ADT

    用如下网址,将xx.x.x替换为想要的版本号,通过迅雷等新建下载输入如下网址,再通过离线安装,稳! http://dl.google.com/android/ADT-xx.x.x.zip 来自http ...

  5. Valgrind的多线程调试工具

    Valgrind的多线程调试工具  Helgrind是Valgrind的一个重点功能 本节主要针对与多线程基本安全问题进行检测:[所有的代码环境都是在POSIX_THREAD模式下] 写线程代码时 经 ...

  6. Mini2440 LED驱动程序设计

    1 LED初始化: 2 LED闪烁设计 位或操作:| 取反操作:~ 位与操作:& http://www.tuicool.com/articles/eQzEJv

  7. Qt线程(2) QThread中使用WorkObject

    一般继承QThread的WorkThread都会在重载的run()中创建临时的WorkObject,这样能确定这个WorkObject在该thread中使用 那如果这个WorkObject是个Sing ...

  8. LintCode First Bad Version

    找出第一个出问题的version. /** * public class SVNRepo { * public static boolean isBadVersion(int k); * } * yo ...

  9. jquery dataTables.min.js API

    demo: http://datatables.net/release-datatables/examples/api/select_single_row.html 选择一行http://datata ...

  10. yii使用MongoDB作为数据库服务软件[win7环境下](1)

    1.进入http://php.net,在站内搜索栏搜索mongodb,查看相关的安装步骤信息. 2.找到相应的php.ini配置文件,使用wampserver等服务器软件时,千万不要找错了php.in ...