二叉树中和为某一值的路径、复杂链表的复制(剑指offer 24-25)c++版本

#include 
#include 

using namespace std;

struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
	TreeNode(int x) :
		val(x), left(NULL), right(NULL) {
	}
};
struct RandomListNode {
	int label;
	struct RandomListNode *next, *random;
	RandomListNode(int x) :
		label(x), next(NULL), random(NULL) {
	}
};
class Solution {
public:
	//JZ24 二叉树中和为某一值的路径
	vector<vector<int> > path;
	vector<int> temp;
	vector<vector<int> > FindPath(TreeNode* root, int expectNumber);
	//JZ25 复杂链表的复制
	RandomListNode* Clone(RandomListNode* pHead);
	void cloneTable(RandomListNode* pHead);
	void dealRandom(RandomListNode* pHead);
	RandomListNode* splitTable(RandomListNode* pHead);
};
//JZ24
vector<vector<int> > Solution::FindPath(TreeNode* root, int expectNumber) {
	//回溯法
	if (!root)	return path;
	temp.push_back(root->val);
	if (root->val == expectNumber) {
		bool isleaf = (root->left == nullptr) && (root->right == nullptr);
		if (isleaf)
			path.push_back(temp);
	}
	if (root->left) {
		FindPath(root->left, expectNumber - root->val);
	}
	if (root->right) {
		FindPath(root->right, expectNumber - root->val);
	}
	temp.pop_back();
	return path;
}
//JZ25
RandomListNode* Solution::Clone(RandomListNode* pHead) {
	//初始链表为1->2->3,先复制链表为1->1'->2->2'->3->3';
	//再处理random指针,1'的random指针指向1->random->next;接着拆分为两个链表
	if (!pHead)	return nullptr;
	cloneTable(pHead);
	dealRandom(pHead);
	return splitTable(pHead);
}
void Solution::cloneTable(RandomListNode* pHead) {
	RandomListNode* pNode = pHead;
	while (pNode) {
		RandomListNode* temp = new RandomListNode(0);
		temp->label = pNode->label;
		temp->next = pNode->next;
		pNode->next = temp;
		pNode = temp->next;
	}
	return;
}
void Solution::dealRandom(RandomListNode* pHead) {
	RandomListNode* pNode = pHead;
	while (pNode) {
		if (pNode->random)
			pNode->next->random = pNode->random->next;
		pNode = pNode->next->next;
	}
	return;
}
RandomListNode* Solution::splitTable(RandomListNode* pHead) {
	RandomListNode* result = pHead->next;
	RandomListNode* pinit = pHead;
	RandomListNode* presult = result;
	pHead->next = result->next;
	while (pinit) {
		pinit->next = presult->next;
		if (pinit->next)
			presult->next = pinit->next->next;
		pinit = pinit->next;
		presult = presult->next;
	}
	return result;
}


//JZ24
void test1() {
	TreeNode* root = new TreeNode(1);
	root->left = new TreeNode(2);
	root->right = new TreeNode(3);
	root->left->left = new TreeNode(4);
	root->left->right = new TreeNode(5);
	root->right->right = new TreeNode(3);
	Solution s;
	s.FindPath(root, 7);
	int len = s.path.size();
	for (int i = 0; i < len; i++) {
		for (vector<int>::iterator it = s.path[i].begin(); it != s.path[i].end(); it++)
			cout << *it << ' ';
		cout << endl;
	}
	return;
}
//JZ25
void test2() {
	RandomListNode* pHead = new RandomListNode(1);
	RandomListNode* pnext = new RandomListNode(2);
	pHead->next = pnext;
	RandomListNode* pnnext = new RandomListNode(3);
	pnext->next = pnnext;
	pHead->random = pnnext;
	Solution s;
	cout << s.Clone(pHead)->random->label;
	return;
}

int main() {
	//test1();
	test2();
	system("pause");
	return 0;
}

你可能感兴趣的:(剑指offer,算法)