二叉树前中后/层次遍历的递归与非递归形式(c++)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | /* 二叉树前中后/层次遍历的递归与非递归形式 */ //*************** void preOrder1(BinaryTreeNode* pRoot) { if (pRoot==NULL) return ; cout<<pRoot->value; if (pRoot->left!=NULL) preOrder1(pRoot->left); if (pRoot->right!=NULL) preOrder1(pRoot->right); } void preOrder2(BinaryTreeNode* pRoot) { stack<BinaryTreeNode*> s; BinaryTreeNode *p=pRoot; if (pRoot==NULL) return ; while (p!=NULL||!s.empty()) { while (p!=NULL) { cout<<p->value<< " " ; s.push(p); p=p->left; } if (!s.empty()) { p=s.top(); s.pop(); p=p->right; } } } void preOrder2(BinaryTreeNode* pRoot){ if (pRoot==NULL) return ; stack<BinaryTreeNode*> s; s.push(root); BinaryTreeNode* p; while (!s.empty()){ p = s.top(); cout<<p->data; //遍历根结点 s.pop(); if (p>right){ s.push(p->right); //先将右子树压栈 } if (p->left){ s.push(p->left); //再将左子树压栈 } } } //***************** //中序遍历 void inOrder1(BinaryTreeNode* pRoot) { if (pRoot==NULL) return ; if (pRoot->left!=NULL) inOrder1(pRoot->left); cout<<pRoot->value; if (pRoot->right!=NULL) inOrder1(pRoot->right); } void inOrder2(BinaryTreeNode* pRoot) { stack<BinaryTreeNode*> s; BinaryTreeNode *p=pRoot; if (pRoot==NULL) return ; while (p!=NULL||!s.empty()) { while (p!=NULL) { s.push(p); p=p->left; } if (!s.empty()) { p=s.top(); cout<<p->value<< " " ; s.pop(); p=p->right; } } } //***************** //后序遍历 void postOrder1(BinaryTreeNode* pRoot) { if (pRoot==NULL) return ; postOrder1(pRoot->left); postOrder1(pRoot->right); cout<<pRoot->value<< " " ; } void postOrder2(BinaryTreeNode* pRoot) { stack<BinaryTreeNode*> s; BinaryTreeNode *cur; BinaryTreeNode *pre=NULL; //记录上一个输出的节点 s.push(pRoot); //根结点入栈 while (!s.empty()) { cur=s.top(); if ((cur->left==NULL&&cur->right==NULL)||(pre!=NULL&&(pre==cur->left||pre==cur->right))) { //左孩子和右孩子同时为空,或者当前结点的左孩子或右孩子已经遍历过了 cout<<cur->value<< " " ; s.pop(); pre=cur; } else { if (cur->right!=NULL) s.push(cur->right); if (cur->left!=NULL) s.push(cur->left); } } } //***************** //层次遍历,使用队列 void PrintFromTopToBottom(BinaryTreeNode* pRoot) { if (pRoot == NULL) return ; deque<BinaryTreeNode *> dequeTreeNode; dequeTreeNode.push_back(pRoot); while (dequeTreeNode.size()) { BinaryTreeNode *pNode = dequeTreeNode.front(); dequeTreeNode.pop_front(); cout<<pNode->m_nValue<< " " ; if (pNode->m_pLeft) dequeTreeNode.push_back(pNode->m_pLeft); if (pNode->m_pRight) dequeTreeNode.push_back(pNode->m_pRight); } } //深度优先遍历~先序遍历 void dfs(BinaryTreeNode* root){ stack<BinaryTreeNode* *> nodeStack; nodeStack.push(root); Node *node; while (!nodeStack.empty()){ node = nodeStack.top(); cout<<node->data; //遍历根结点 nodeStack.pop(); if (node->rchild){ nodeStack.push(node->rchild); //先将右子树压栈 } if (node->lchild){ nodeStack.push(node->lchild); //再将左子树压栈 } } } //广度优先遍历~层次遍历 void bfs(BinaryTreeNode* root){ queue<BinaryTreeNode* *> nodeQueue; nodeQueue.push(root); Node *node; while (!nodeQueue.empty()){ node = nodeQueue.front(); nodeQueue.pop(); cout<<node->data; //遍历根结点 if (node->lchild){ nodeQueue.push(node->lchild); //先将左子树入队 } if (node->rchild){ nodeQueue.push(node->rchild); //再将右子树入队 } } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步