Construct Binary Tree from Inorder and Postorder Traversal

原创 2016年08月31日 13:38:40

一、问题描述

Given inorder and postorder traversal of a tree, construct the binary tree.

二、思路

Construct Binary Tree from Preorder and Inorder Traversal思路基本一致,主要是根据在中序遍历中找到当前的值,然后通过对应关系递归。

三、代码

/**
 * 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) {
        return creatTree(0, inorder.size() - 1, 0, postorder.size() - 1, inorder, postorder);
    }
    TreeNode*creatTree(int in_begin, int in_end, int post_begin,int post_end, vector<int> &inorder, vector<int> &postorder){
        if(post_begin > post_end)    return NULL;
        TreeNode* root = new TreeNode(postorder[post_end]);
        int index = 0;
        for(int i = in_end; i >= 0; --i){
            if(inorder[i] == root -> val){
                index = i;
                break;
            }
        }
        root -> right = creatTree(index + 1, in_end, post_begin + index - in_begin, post_end - 1, inorder, postorder);
        root -> left = creatTree(in_begin, index - 1, post_begin, post_begin + index - in_begin - 1, inorder, postorder);
        return root;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Construct Binary Tree from Inorder and Postorder Traversal

题目 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu...
  • Boyhou
  • Boyhou
  • 2013年07月28日 15:53
  • 812

Construct Binary Tree from Inorder and Postorder Traversal(medium)

【题目】             Given inorder and postorder traversal of a tree, construct the binary tree. ...
  • haihaa
  • haihaa
  • 2016年05月23日 10:35
  • 99

【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
  • 316
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Construct Binary Tree from Inorder and Postorder Traversal
举报原因:
原因补充:

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