leetcode-Construct Binary Tree from Inorder and Postorder Traversal

原创 2013年12月04日 20:13:24

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.empty())
            return NULL;
        return buildTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
    }
private:
    TreeNode *buildTree(vector<int> &inorder, int istart, int iend, 
                     vector<int> &postorder, int pstart, int pend) {
        if (istart == iend)
            return new TreeNode(inorder[istart]);
        
        int i;
        for (i=istart; i<=iend; ++i) {
            if (inorder[i]==postorder[pend]) {
                break;
            }
        }
        
        TreeNode *root = new TreeNode(inorder[i]);
        
        if (i>istart)
            root->left = buildTree(inorder, istart, i-1, postorder, pstart, pstart+i-istart-1);
        
        if (i<iend)
            root->right = buildTree(inorder, i+1, iend, postorder, pstart+i-istart, pend-1);
        
        return root;
    }
    
};


相关文章推荐

【LEETCODE】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 t...

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

[LeetCode]*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 tha...

LeetCode#106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder travers...

【LeetCode】Construct Binary Tree from (Preorder or Postorder) and Inorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal

LeetCode Construct Binary Tree from Inorder and Postorder Traversal 思考分析过程分享

思考: 1 画图: 把二叉树和其中序和后序的序列都画出来(这个很重要,我以前有时候总是凭空想象,结果脑子一片空白) 2 把问题写下来:怎么构造一个节点? 3 观察: 到底有什么特点是可以利用来解题的呢...

Leetcode_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 tha...

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 中序和后序恢复二叉树

1 解题思想这道题和105的思想,基本就是一致的,这里将会说的比较简单,之说下不同 Leetcode 105. Construct Binary Tree from Preorder and Ino...
  • MebiuW
  • MebiuW
  • 2016年09月16日 16:42
  • 313

【Leetcode】Construct Binary Tree From Inorder and Preorder/Postorder Traversal

【题目】 Given preorder and inorder traversal of a tree, construct the binary tree. 【思路】 Hint: A...

【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)】

【106-Construct Binary Tree from Inorder and Postorder Traversal(通过中序和后序遍历构造二叉树II)】【LeetCode-面试算法经典-J...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode-Construct Binary Tree from Inorder and Postorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)