三种遍历递归及非递归实现、二分查找法

以下三种遍历递归及非递归实现、二分查找法,要特别熟悉,写代码时一气呵成,中停不要有半点停顿。

二分查找法:

//-----------------------------------------------------
//Binary Search
//-----------------------------------------------------
bool binarySearch(int a[], int start, int end,int val) {
	int low = start, high = end;
	
	while (low <= high) {
		int mid = (low + start) / 2;
		if (a[mid] == val)
			return true;
		else if (a[mid] < val)
			low = mid + 1;
		else
			high = mid - 1;
	}
	return false;
}
前序遍历:
//-----------------------------------------------------
//preTraversal
//-----------------------------------------------------
//递归实现
vector<int> preOrder(TreeNode* root,vector<int>& res) {
	if (!root)
		return res;
	res.push_back(root->val);
	if (root->left)
		preOrder(root->left,res);
	if (root->right)
		preOrder(root->right,res);
	return res;
}
//非递归实现
vector<int> preOrder(TreeNode* root) {
	vector<int> res;
	if (!root)
		return res;
	stack<TreeNode*> sta;
	
	sta.push(root);
	while (!sta.empty()) {
		TreeNode* cur = sta.top();
		sta.pop();
		res.push_back(cur->val);
		if (cur->right)
			sta.push(cur->right);
		if (cur->left)
			sta.push(cur->left);
	}
	return res;
}
中序遍历:
struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int x) :val(x), left(nullptr), right(nullptr) {}
};
//-----------------------------------------------------
//InOrderTraversal
//-----------------------------------------------------
//递归实现
vector<int> inOrder(TreeNode* root, vector<int>& res) {
	if (!root)
		return res;
	if (root->left)
		preOrder(root->left, res);
	res.push_back(root->val);
	if (root->right)
		preOrder(root->right, res);
	return res;
}
//非递归实现
vector<int> inOrder(TreeNode* root) {
	vector<int> res;
	if (!root)
		return res;
	stack<TreeNode*> sta;
	//sta.push(root);
	TreeNode* cur = root;
	while (!sta.empty() || cur) {
		if (cur) {
			sta.push(cur);
			cur = cur->left;
		}
		else {
			cur = sta.top();
			sta.pop();
			res.push_back(cur->val);
			cur = cur->right;
		}
	}
	return res;
}
后序遍历:(有点难,但只要记住注释文字,还是很好写出来的)
struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int x) :val(x), left(nullptr), right(nullptr) {}
};
//-----------------------------------------------------
//postOrderTraversal
//-----------------------------------------------------
//递归实现
vector<int> postOrder(TreeNode* root, vector<int>& res) {
	if (!root)
		return res;
	if (root->left)
		postOrder(root->left, res);
	if (root->right)
		postOrder(root->right, res);
	res.push_back(root->val);
	return res;
}
//非递归实现
vector<int> postOrder(TreeNode* root) {
	vector<int> res;
	if (!root)
		return res;
	stack<TreeNode*> sta;
	TreeNode* cur = root;
	TreeNode* pre = nullptr;
	sta.push(root);
	while (!sta.empty()) {
		cur = sta.top();
		if (cur->left == nullptr&&cur->right == nullptr ||//此条件为真:左右儿子为空或pre不为空且pre为左或右儿子
			pre != nullptr && (pre == cur->left || pre == cur->right)) {
			res.push_back(cur->val);
			pre = cur;//此语句容易忘记
			sta.pop();
		}else{
			if (cur->right)
				sta.push(cur->right);
			if (cur->left)
				sta.push(cur->left);
		}
	}
	return res;
}

猜你喜欢

转载自blog.csdn.net/tt_love9527/article/details/80615910
今日推荐