中序遍历
基于递归实现
void inorderTraversal1(TreeNode* tree,std::vector<int>& vec) {
if (tree == NULL) return;
inorderTraversal1(tree->left, vec);
vec.push_back(tree->val);
inorderTraversal1(tree->right, vec);
}
非递归实现之stack
void inorderTraversal2(TreeNode* tree, std::vector<int>& vec) {
if (tree == NULL) return;
std::stack<TreeNode*> stack;
TreeNode* cur = tree;
while (!stack.empty() || cur != NULL) {
if (cur != NULL) {
stack.push(cur);
cur = cur->left;
}
else {
cur = stack.top();
stack.pop();
vec.push_back(cur->val);
cur = cur->right;
}
}
}
非递归实现之线索树
void inorderTraversal3(TreeNode* tree, std::vector<int>& vec) {
if (tree == NULL) return;
TreeNode* cur = tree;
while (cur != NULL) {
if (cur->left != NULL) {
TreeNode* lmr = cur->left;
while (lmr->right != NULL&&lmr->right!=cur) lmr = lmr->right;
if (lmr->right == NULL) {
lmr->right = cur;
cur = cur->left;
}
else {
lmr->right = NULL;
vec.push_back(cur->val);
cur = cur->right;
}
}
else {
vec.push_back(cur->val);
cur = cur->right;
}
}
}
先序遍历
基于递归实现
void preorderTraversal1(TreeNode* tree, std::vector<int>& vec) {
if (tree == NULL) return;
vec.push_back(tree->val);
preorderTraversal1(tree->left, vec);
preorderTraversal1(tree->right, vec);
}
非递归实现之stack
void preorderTraversal2(TreeNode* tree, std::vector<int>& vec) {
if (tree == NULL) return;
std::stack<TreeNode*> stack;
TreeNode* cur = tree;
while (!stack.empty() || cur != NULL) {
if (cur != NULL) {
stack.push(cur);
vec.push_back(cur->val);
cur = cur->left;
}
else {
cur = stack.top();
stack.pop();
cur = cur->right;
}
}
}
非递归实现之线索树
void preorderTraversal3(TreeNode* tree, std::vector<int>& vec) {
if (tree == NULL) return;
TreeNode* cur = tree;
while (cur != NULL) {
if (cur->left != NULL) {
TreeNode* lmr = cur->left;
while (lmr->right != NULL && lmr->right != cur) lmr = lmr->right;
if (lmr->right == NULL) {
lmr->right = cur;
vec.push_back(cur->val);
cur = cur->left;
}
else {
lmr->right = NULL;
cur = cur->right;
}
}
else {
vec.push_back(cur->val);
cur = cur->right;
}
}
}
后序遍历
基于递归实现
void postorderTraversal1(TreeNode* tree, std::vector<int>& vec) {
if (tree == NULL) return;
postorderTraversal1(tree->left, vec);
postorderTraversal1(tree->right, vec);
vec.push_back(tree->val);
}
非递归实现之stack
void postorderTraversal2(TreeNode* tree, std::vector<int>& vec) {
if (tree == NULL) return;
std::stack<TreeNode*> stack;
TreeNode* cur = tree;
while (!stack.empty() || cur != NULL) {
if (cur != NULL) {
stack.push(cur);
vec.push_back(cur->val);
cur = cur->right;
}
else {
cur = stack.top();
stack.pop();
cur = cur->left;
}
}
std::reverse(vec.begin(), vec.end());
}
非递归实现之线索树
void postorderTraversal3(TreeNode* tree, std::vector<int>& vec) {
if (tree == NULL) return;
TreeNode* cur = tree;
while (cur != NULL) {
if (cur->right != NULL) {
TreeNode* rml = cur->right;
while (rml->left != NULL && rml->left != cur) rml = rml->left;
if (rml->left == NULL) {
rml->left = cur;
vec.push_back(cur->val);
cur = cur->right;
}
else {
rml->left = NULL;
cur = cur->left;
}
}
else {
vec.push_back(cur->val);
cur = cur->left;
}
}
std::reverse(vec.begin(), vec.end());
}