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)的更多相关文章

  1. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

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

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

  3. 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: ...

  4. Construct Binary Tree from Inorder and Postorder Traversal

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

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

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

  7. 【题解二连发】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 ...

  8. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

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

  9. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

随机推荐

  1. 汉诺塔的c++实现

    void hanNuoTa(int n,int a,int b,int c) { ) return; hanNuoTa(n - , a, c, b); cout << n << ...

  2. Python中的变量和常量

    本文主要介绍Python中的变量和常量,包括变量的命名规范,使用注意事项 -------------- 完美的分割线 --------------- 1.变量 1.1.变量理解 1)什么是变量 变量即 ...

  3. 深度学习 循环神经网络 LSTM 示例

    最近在网上找到了一个使用LSTM 网络解决  世界银行中各国 GDP预测的一个问题,感觉比较实用,毕竟这是找到的唯一一个可以正确运行的程序. #encoding:UTF-8 import pandas ...

  4. 三网合一 中国移动铁通光猫 HG6821M 如何设置宽带自动连接

    假期炎热,都说大连是海滨城市比较凉爽,但是那地方潮气太大,实在是不太好呆,于是乎我打道回府来到了内陆家中. 回到家中发现老式的光猫被替换成了  最新的  HG6821M  . 按照铭牌上的说明,登录. ...

  5. UVAlive5713 Qin Shi Huang's National Road System【次小生成树】【DP】

    LINK1 LINK2 题目大意 给你平面上的n个点 每个点有一个权值 让你求出一个生成树 可以选择一条边不花费代价 要最大化这条边两边端点的权值/剩下n-2条边的长度之和 思路 发现发现其实端点权值 ...

  6. POJ3177 Redundant Paths【tarjan边双联通分量】

    LINK 题目大意 给你一个有重边的无向图图,问你最少连接多少条边可以使得整个图双联通 思路 就是个边双的模板 注意判重边的时候只对父亲节点需要考虑 你就dfs的时候记录一下出现了多少条连向父亲的边就 ...

  7. Redis学习笔记-常用命令篇(Centos7)

    redis提供了丰富的命令,这些命令可以在linux终端使用.在各类语言中,这些命令都有对应的方法. 一.键值相关 1.keys 返回满足给定pattern的所有key 127.0.0.1:6379& ...

  8. 【java基础】java字符串之StringBuffer和StringBuilder

    [一]简述区别 package com.sxf.test.string; public class StringBufferStringBuilderTest { public static void ...

  9. js的拼接

    var datatr = " <tr>"; datatr += "<td bgcolor='#EEEEEE'><input class='i ...

  10. python 简明教程

    第一个源程序 #!/usr/bin/python# Filename : helloworld.pyprint 'Hello world' 执行: $ python helloworld.py 或者  ...