leetcode106. 从中序与后序遍历序列构造二叉树

根据一棵树的中序遍历与后序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]

返回如下的二叉树:

    3
   / \
  9  20
    /  \
   15   7

思路:

递归的思想

后序的最后一个就是根节点,在中序里面找到这个节点,递归这个过程

边界条件

  • 空的,必须判断空的vector
  • 只有2个【2,1】;【2,1】这样就必须判断左右长度

 

方法1:

可以申请临时vector,这样占内存比较多,但是不用调用子函数

方法2:

直接在vector上求解,用左右指针进行处理,这样必须调用子函数buildTreeCoreFun把形参left,right放进去。

 

/**
 * 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 *buildTreeCoreFun(vector<int>& inorder,int InorderLeft,int InorderRight,vector<int>& postorder,int PostorderLeft,int PostorderRight)
    {
        if (inorder.size()==0)
		    return NULL;
        TreeNode* tree=new TreeNode(postorder[PostorderRight]);
	    if (PostorderRight==PostorderLeft||InorderLeft==InorderRight)
            return tree;
        vector<int>::iterator it=find(inorder.begin()+InorderLeft,inorder.begin()+InorderRight,tree->val);
        int position=0;
        if (it!=inorder.end())
            position=distance(inorder.begin(),it);
        int lenLeft=position-InorderLeft;
        int lenRight=InorderRight-position;
        if(lenLeft)
            tree->left	=buildTreeCoreFun(inorder,InorderLeft,InorderLeft+lenLeft-1,postorder,PostorderLeft,PostorderLeft+lenLeft-1);
        if(lenRight)
            tree->right	=buildTreeCoreFun(inorder,InorderRight-lenRight+1,InorderRight,postorder,PostorderRight-lenRight,PostorderRight-1);
        return tree;
    }

    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        int InorderLeft,InorderRight;
        int PostorderLeft,PostorderRight;
        InorderLeft=PostorderLeft=0;
        InorderRight=PostorderRight=postorder.size()-1;
        return buildTreeCoreFun(inorder,InorderLeft,InorderRight,postorder,PostorderLeft,PostorderRight);
    }
};

 

 

调试代码:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode *buildTreeCoreFun(vector<int>& inorder,int InorderLeft,int InorderRight,vector<int>& postorder,int PostorderLeft,int PostorderRight)
{
	if (inorder.size()==0)
		return NULL;
	TreeNode* tree=new TreeNode(postorder[PostorderRight]);
	if (PostorderRight==PostorderLeft||InorderLeft==InorderRight)
		return tree;
	vector<int>::iterator it=find(inorder.begin()+InorderLeft,inorder.begin()+InorderRight,tree->val);
	int position=0;
	if (it!=inorder.end())
		position=distance(inorder.begin(),it);
	int lenLeft=position-InorderLeft;
	int lenRight=InorderRight-position;
	if(lenLeft)
		tree->left	=buildTreeCoreFun(inorder,InorderLeft,InorderLeft+lenLeft-1,postorder,PostorderLeft,PostorderLeft+lenLeft-1);
	if(lenRight)
		tree->right	=buildTreeCoreFun(inorder,InorderRight-lenRight+1,InorderRight,postorder,PostorderRight-lenRight,PostorderRight-1);
	return tree;
}

TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
	int InorderLeft,InorderRight;
	int PostorderLeft,PostorderRight;
	InorderLeft=PostorderLeft=0;
	InorderRight=PostorderRight=postorder.size()-1;
	return buildTreeCoreFun(inorder,InorderLeft,InorderRight,postorder,PostorderLeft,PostorderRight);
}

int main()
{
/*	int arr[5]={9,3,15,20,7};
	int arr1[5]={9,15,7,20,3};
	vector<int> inorder(arr,arr+5);
	vector<int> posorder(arr1,arr1+5);*/
	int arr[2]={2,1};
	int arr1[2]={2,1};
	vector<int> inorder(arr,arr+2);
	vector<int> posorder(arr1,arr1+2);
	TreeNode* root=buildTree(inorder,posorder);
	int i=0;
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值