`

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) {}
 * };
 */
class Solution {
public:
    TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
        if(inorder.size() != postorder.size()) return NULL;
        int n = inorder.size();
        return build(inorder, 0, postorder, n-1, n);
    }
    
    TreeNode *build(vector<int> &io, int inStart, vector<int> &po, int postEnd, int len) {
        if(inStart < 0 || inStart + len > io.size() || postEnd >= po.size() || postEnd - len < -1 || len < 1) {
            return NULL;
        }
        
        int rootVal = po[postEnd];
        
        int i = inStart;
        while(io[i] != rootVal) ++i;
        
        TreeNode *root = new TreeNode(rootVal);
        
        int leftLen = i - inStart;
        int rightLen = len - leftLen - 1;
        
        root->right = build(io, i+1, po, postEnd - 1, rightLen);
        root->left = build(io, inStart, po, postEnd - rightLen - 1, leftLen);
        
        return root;
    }
};

 

 

欢迎关注微信公众号——计算机视觉:

 

0
0
分享到:
评论

相关推荐

    Leetcode 使用 Javascript 的解决方案.zip

    Javascript 的解决方案Leetcode Problems and interview problems in ...Construct Binary Tree from Inorder and Postorder Traversal.js107 Binary Tree Level Order Traversal II.js108 Convert Sorted Array to ...

    小象学院2020 刷题班 3.2-4.171

    14. **重建二叉树**(Construct Binary Tree from Inorder and Postorder Traversal / Preorder Traversal) - 知识点:二叉树,递归,序列化与反序列化 - 解题策略:根据中序和后序/前序遍历序列,通过递归构建...

    LeetCode最全代码

    421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...

    四平方和定理leetcode-leetcode-practice:个人LeetCode练习代码

    106.construct-binary-tree-from-inorder-and-postorder-traversal (从中序与后序遍历序列构造二叉树) 112.path-sum (路径总和) 116.populating-next-right-pointers-in-each-node (填充每个节点的下一个右侧节点

    lrucacheleetcode-LeetCode_Note:leetcode个人笔记

    lru缓存leetcode LeetCode_Note leetcode 个人笔记 ...[106_construct-binary-tree-from-inorder-and-postorder-traversal.cpp] [107_binary-tree-level-order-traversal-ii.cpp] [108_convert-sorted

    leetcode跳跃-Algorithm:算法学习,包括leetcode算法题,

    leetcode 跳跃 Algorithm 算法题解: 包括书籍算法, 程序员算法面试指南, 还有leetcode算法题 ...construct-binary-tree-from-inorder-and-postorder-traversal 无官方题解 116 populating-next-right-pointers-in-eac

    复旦上机真题

    Node root = buildTree(inorder, postorder, 0, inorder.length - 1); System.out.println(levelOrder(root)); } } ``` #### 三、复旦专硕2010年机试试题分析 ##### 题目描述 从文件中读取数据构建二叉树,并...

    图解数据结构6-树及树的遍历

    - **中序遍历(Inorder Traversal)**:先访问左子树,然后访问根节点,最后访问右子树。这种遍历方式对于二叉搜索树特别有用,因为结果是按值排序的。 - **后序遍历(Postorder Traversal)**:先访问左子树,然后访问...

    PHP Class&amp;Object -- 解析PHP实现二叉树

    2. 中序遍历(Inorder Traversal):首先递归地进行中序遍历左子树,然后访问根节点,最后递归地进行中序遍历右子树。对于二叉搜索树(BST),中序遍历能以升序访问树中的所有节点。 3. 后序遍历(Postorder ...

Global site tag (gtag.js) - Google Analytics