> For the complete documentation index, see [llms.txt](https://jenhsuan.gitbook.io/algorithm/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jenhsuan.gitbook.io/algorithm/leetcode/106.-construct-binary-tree-from-inorder-and-postorder-traversal.md).

# 106. Construct Binary Tree from Inorder and Postorder Traversal

## 1.問題&#x20;

![](/files/-LOSZUzhfTMAVimGMjbN)

## 2.想法 <a href="#id-2-xiang-fa" id="id-2-xiang-fa"></a>

* 提問
  * 確認題意:  root是NULL是true或false?
* function header, parameter
* r input
* 說明想法
  * 想法與105題相同, 由於postorder序列建構tree, 必須由序列的最後往前
* 測試計算複雜度

## **3.程式碼** <a href="#id-3-cheng-shi" id="id-3-cheng-shi"></a>

```
/**
 * Definition for a binary tree node.
 * 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) {
        int pos = postorder.size() - 1;
        return buildBinary(inorder, postorder, 0, postorder.size() - 1, pos);
    }
private:
    TreeNode* buildBinary(vector<int>& inorder, vector<int>& postorder, int left, int right, int& pos) {
        if (pos < 0 || left > right) {
            return NULL;
        }
        int i = 0;
        for (i = left; i <= right; i++) {
            if (inorder[i] == postorder[pos]){
                break;
            }
        }
        TreeNode* node = new TreeNode(postorder[pos]);
        pos--;
        node->right = buildBinary(inorder, postorder, i + 1, right, pos);
        node->left = buildBinary(inorder, postorder, left, i - 1, pos);
        
        return node;
    }
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/106.-construct-binary-tree-from-inorder-and-postorder-traversal.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
