36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
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的更多相关文章
- 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 ...
- 【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 ...
- 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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [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 ...
- 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】
我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列, ======O=========== 后序遍 ...
- [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 ...
- 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 ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- Java类的成员函数调用顺序
class A { public A() { System.out.println("----------A 构造-------------"); } static void sb ...
- 常用dos命令 如查询端口号是否被占用
①查询端口号是否被占用掉 在windows命令行窗口下执行:运行--cmdC:\>netstat -aon|findstr "8080" TCP 127.0.0.1:80 0 ...
- Java String.split()用法小结
在java.lang包中有String.split()方法,返回是一个数组 我在应用中用到一些,给大家总结一下,仅供大家参考: 1.如果用“.”作为分隔的话,必须是如下写法,String.split( ...
- Get请求编码
遇到get请求中文编码的时候,有的人会采用在Tomcat/conf/server.xml中的如下添加一句: <Connector port="8080" protocol=& ...
- Python的平凡之路(11)
一. rabbitmq 1 进程Queue: 父进程与子进程进行交互,或者同属于同一父进程下多个子进程进行交互 2 队列通信: send1.py #!/usr/bin/env python#Au ...
- Javascript中封装window.open的例子
对window.open进行封装, 使其更好用, 且更兼容, 很多人说window.open不兼容,其实不是, 因为不能直接执行, 必须通过用户手动触发才行;看代码: 代码如下 复制代码 var op ...
- HDU 2089 不要62
也是简单的数位dp. #include<iostream> #include<cstdio> #include<cstring> #include<algor ...
- ARM compiler No such file or directory
/********************************************************************************* * ARM compiler No ...
- source insight 相对路径
source insight项目 在移动到另外一个地方时,会因为之前是绝对路径而导致,项目中的文件都不可用,需要重新把这些文件添加一遍. 这是个令人讨厌的事情. 解决办法为创建项目时设定为绝对路径.方 ...
- Js控制滚动条
1>全局控制 //向上滑动显示 var initTop = 0; var i = 1; $(window).scroll(function(){ var scrollTop = $(docume ...