以下三种遍历递归及非递归实现、二分查找法,要特别熟悉,写代码时一气呵成,中停不要有半点停顿。
二分查找法:
//-----------------------------------------------------
//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;
}