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

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

二分查找法:

//-----------------------------------------------------
//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;
}

前序遍历:

struct TreeNode {
	int val;
	TreeNode* left;
	TreeNode* right;
	TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};
//-----------------------------------------------------
//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();
            //此条件为真:左右儿子为空或pre不为空且pre为左或右儿子
		if (cur->left == nullptr&&cur->right == nullptr ||
			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;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值