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.

思想: 迭代。

说明: 这类问题,要求一个提供根节点,然后另一个序列(中序序列)可依据根节点分出左右子树。

  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. TreeNode *createTree(vector<int> &inorder, int start, int end, vector<int> &postorder, int start2, int end2) {
  11. if(start > end || start2 > end2) return NULL;
  12. TreeNode *root = new TreeNode(postorder[end2]);
  13. int i;
  14. for(i = start; i <= end; ++i)
  15. if(inorder[i] == postorder[end2]) break;
  16. // if(i > end) throws std::exception("error");
  17. root->left = createTree(inorder, start, i-1, postorder, start2, start2 + i-start-1);
  18. root->right = createTree(inorder, i+1, end, postorder, start2+i-start, end2-1);
  19. return root;
  20. }
  21.  
  22. class Solution {
  23. public:
  24. TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
  25. return createTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
  26. }
  27. };

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. * 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. TreeNode* createTree(vector<int> &preorder, int start, int end, vector<int> &inorder, int start2, int end2) {
  11. if(start > end || start2 > end2) return NULL;
  12. TreeNode *root = new TreeNode(preorder[start]);
  13. int i;
  14. for(i = start2; i <= end2; ++i)
  15. if(preorder[start] == inorder[i]) break;
  16. root->left = createTree(preorder, start+1, start+i-start2, inorder, start2, i-1);
  17. root->right = createTree(preorder, start+i-start2+1, end, inorder, i+1, end2);
  18. return root;
  19. }
  20. class Solution {
  21. public:
  22. TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
  23. return createTree(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1);
  24. }
  25. };

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. Java类的成员函数调用顺序

    class A { public A() { System.out.println("----------A 构造-------------"); } static void sb ...

  2. 常用dos命令 如查询端口号是否被占用

    ①查询端口号是否被占用掉 在windows命令行窗口下执行:运行--cmdC:\>netstat -aon|findstr "8080" TCP 127.0.0.1:80 0 ...

  3. Java String.split()用法小结

    在java.lang包中有String.split()方法,返回是一个数组 我在应用中用到一些,给大家总结一下,仅供大家参考: 1.如果用“.”作为分隔的话,必须是如下写法,String.split( ...

  4. Get请求编码

    遇到get请求中文编码的时候,有的人会采用在Tomcat/conf/server.xml中的如下添加一句: <Connector port="8080" protocol=& ...

  5. Python的平凡之路(11)

    一. rabbitmq 1 进程Queue:  父进程与子进程进行交互,或者同属于同一父进程下多个子进程进行交互 2 队列通信:   send1.py #!/usr/bin/env python#Au ...

  6. Javascript中封装window.open的例子

    对window.open进行封装, 使其更好用, 且更兼容, 很多人说window.open不兼容,其实不是, 因为不能直接执行, 必须通过用户手动触发才行;看代码: 代码如下 复制代码 var op ...

  7. HDU 2089 不要62

    也是简单的数位dp. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...

  8. ARM compiler No such file or directory

    /********************************************************************************* * ARM compiler No ...

  9. source insight 相对路径

    source insight项目 在移动到另外一个地方时,会因为之前是绝对路径而导致,项目中的文件都不可用,需要重新把这些文件添加一遍. 这是个令人讨厌的事情. 解决办法为创建项目时设定为绝对路径.方 ...

  10. Js控制滚动条

    1>全局控制 //向上滑动显示 var initTop = 0; var i = 1; $(window).scroll(function(){ var scrollTop = $(docume ...