106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
root = NULL;
if(inorder.empty()) return root;
root = new TreeNode();
buildSubTree(inorder,postorder,,inorder.size()-,,postorder.size()-,root);
return root; }
void buildSubTree(vector<int> &inorder, vector<int>&postorder, int inStartPos, int inEndPos, int postStartPos, int postEndPos,TreeNode * currentNode)
{
currentNode->val = postorder[postEndPos]; //后序遍历的最后一个节点是根节点 //find root position in inorder vector
int inRootPos;
for(int i = inStartPos; i <= inEndPos; i++)
{
if(inorder[i] == postorder[postEndPos])
{
inRootPos = i;
break;
}
} //right tree: 是中序遍历根节点之后的部分,对应后序遍历根节点前相同长度的部分
int newPostPos = postEndPos - max(inEndPos - inRootPos, );
if(inRootPos<inEndPos)
{
currentNode->right = new TreeNode();
buildSubTree(inorder,postorder,inRootPos+,inEndPos,newPostPos,postEndPos-,currentNode->right);
} //leftTree: 是中序遍历根节点之前的部分,对应后序遍历从头开始相同长度的部分
if(inRootPos>inStartPos)
{
currentNode->left = new TreeNode();
buildSubTree(inorder,postorder,inStartPos,inRootPos-,postStartPos,newPostPos-,currentNode->left);
}
}
private:
TreeNode* root;
};
106. Construct Binary Tree from Inorder and Postorder Traversal (Tree; DFS)的更多相关文章
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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: ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 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/cons ...
- LeetCode: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 trav ...
- 【题解二连发】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 - LeetCode Construct Binary ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
随机推荐
- L206
There are so many new books about dying that there are now special shelves set aside forthem in book ...
- json to xml
/* This work is licensed under Creative Commons GNU LGPL License. License: http://creativecommons.or ...
- jQuery 参数详解
url: 要求为String类型的参数,发送请求的地址.如果没有填写, 默认发送当前页的url type: 要求为String类型的参数,请求方式(post或get)默认为get. 注意其他http请 ...
- Alpha阶段敏捷冲刺---Day1
一.Daily Scrum Meeting照片 二.今天冲刺情况反馈 1.昨天已完成的工作 昨天我们组全体成员在五社区五号楼719召开了紧急会议,在会议上我们梳理了编写这个程序的所有流程,并且根 ...
- .Net脱壳工具 de4dot参数说明/简易教程
de4dot /? 帮助原文 使用方法 de4dot "d:\xx.exe" -p xc -p xc 指定壳类型 , 这里是xc,表示Xenocode壳.这样会在exe的相同目录 ...
- Tensorflow 模型文件结构、模型中Tensor查看
tensorflow训练后保存的模型主要包含两部分,一是网络结构的定义(网络图),二是网络结构里的参数值. 1. .meta文件 .meta 文件以 "protocol buffer&qu ...
- Js 手风琴效果
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- Objective-C教程备忘单
终极版本的Objective-C教程备忘单帮助你进行iOS开发. 想开始创建你的第一个iOS应用程序么?那么看一下这篇很棒的教程吧:Create your first iOS 7 Hello Worl ...
- Java进行数据库导出导入 亲测可用
/** * @param hostIP ip地址,可以是本机也可以是远程 * @param userName 数据库的用户名 * @param password 数据库的密码 * @param sav ...
- 《DSP using MATLAB》Problem 2.18
1.代码: function [y, H] = conv_tp(h, x) % Linear Convolution using Toeplitz Matrix % ----------------- ...