剑指offer面试题6之前序中序建立二叉树

1,这是在vs2008上编写的代码。

Bnode.h

#pragma once

class Bnode
{
public:
	Bnode(void);
	~Bnode(void);
	Bnode(int x, Bnode* y, Bnode* z);
	Bnode* construct(int *Pre, int sP, int eP, int *In,int sI, int eI);
private:
	int data;
	Bnode *lchild;
	Bnode *rchild;
};
Bnode.cpp
#include "StdAfx.h"
#include "Bnode.h"

Bnode::Bnode(void)
{
}

Bnode::~Bnode(void)
{
}
Bnode::Bnode(int x, Bnode* y, Bnode* z)
{
	this->data = x;
	this->lchild = y;
	this->rchild = z;
}
Bnode* Bnode::construct(int *Pre, int sP, int eP, int *In, int sI, int eI)
{
	if (sP > eP || sI > eI)//这里好像只要判断它们中的一个就可以了,因为sP和eP间隔应该和sI和eI一样
	{
		return NULL;
	}//这是递归的出口
	//下边是与深层递归的联系
	Bnode* root = new Bnode(Pre[sP], NULL, NULL);//每次递归前序的第一个结点都是现层递归的根节点
	//下边是判断左孩子和右孩子,那得先找到根节点在中序中的位置
	int i;
	for (i = sI; i <= eI; i++)
	{
		if (In[i] == Pre[sP])
		{
			break;
		}
	}//i就是中序中根节点的位置,找到位置后,把根节点左子树的前序和中序,放在下层递归,
	//返回值作本层的左孩子,同理作右孩子
	root->lchild = construct(Pre, sP + 1, sP + i - sI, In, sI, i - 1);
	root->rchild = construct(Pre, sP + i - sI + 1, eP, In, i + 1, eI);
	return root;
}
// 前序中序建二叉树.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "Bnode.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	Bnode A;
	int Pre[] = {1, 2, 4, 7, 3, 5, 6 ,8};
	int In[] = {4, 7, 2, 1, 5, 3, 8, 6};
	Bnode* Root = NULL;
	Root = A.construct(Pre, 0, 7, In, 0, 7);
	system("pause");
	return 0;
}
2,这是在牛客网上编写的代码。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    struct TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> in) {
        if (pre.empty() || in.empty())//只写一个也是可以的
        {
            return NULL;
        }
        TreeNode* root = new TreeNode(pre[0]);
        unsigned int i;
        for (i = 0; i < in.size(); i++)
        {
            if (in[i] == pre[0])
            {
                break;
            }
        }
        vector<int> temp1;//左孩子前序
        unsigned int j;
        for (j = 1; j <= i; j++)
        {
            temp1.push_back(pre[j]);
        }
        vector<int> temp2;//左孩子中序
        for (j = 0; j < i; j++)
        {
            temp2.push_back(in[j]);
        }
        root->left = reConstructBinaryTree(temp1, temp2);
        vector<int> temp3;//右孩子前序
        for (j = i + 1; j < pre.size(); j++)
        {
            temp3.push_back(pre[j]);
        }
        vector<int> temp4;//右孩子中序
        for (j = i + 1; j < in.size(); j++)
        {
            temp4.push_back(in[j]);
        }
        root->right = reConstructBinaryTree(temp3, temp4);
        return root;
    }
};
提交成功!
思考:

代码尽量自己写出来,别用提示的辅助代码!

另外还学到一个成员初始化列表的一些知识:

比如:

class Complex{
private:
	double real;
	double imag;
public:
	Complex(double r, double i);
};
Complex::Complex(double r, double i):real(r),imag(i){};//这就是使用成员初始化列表对成员初始化
//为什么要使用初始化成员列表来初始化成员呢?
//因为c++中某些类型的成员是不允许在构造函数中用赋值语句直接赋值的。
//例如用const修饰的数据成员,或用引用类型的数据成员,只能用成员初始化列表进行初始化.例如:
#include <iostream>
using namespace std;
class A{
public:
	A(int x1):x(x1),rx(x),pi(3.14){};
	void print()
	{
		cout << x << ' ' << rx << ' ' << pi << endl;
	}
private:
	int x;
	int& rx;
	const double pi;
};
int main()
{
	A a(10);
	a.print();
	return 0;
}
//输出结果为:10 10 3.14
//另外注意的是:数据成员是按照它们在类中声明的顺序进行初始化的,
//与它们在成员初始化列表中的顺序无关




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值