二叉树的七种遍历方式以及按行打印
来源:互联网 发布:微淘动态在淘宝哪里找 编辑:程序博客网 时间:2024/06/07 12:18
void preorder_digui(TreeNode* pRoot, vector<int> &preorder_digui_re){ if (pRoot != NULL) { preorder_digui_re.push_back(pRoot->val); if (pRoot->left) preorder_digui(pRoot->left, preorder_digui_re); if (pRoot->right) preorder_digui(pRoot->right, preorder_digui_re); }}void preorder_while(TreeNode* pRoot, vector<int> &preorder_while_re){ stack <TreeNode*>stack1; while (pRoot || !stack1.empty()) { if (pRoot == NULL) { pRoot = stack1.top(); stack1.pop(); } if (pRoot != NULL) { preorder_while_re.push_back(pRoot->val); if (pRoot->right) stack1.push(pRoot->right); pRoot = pRoot->left; } }}void inorder_digui(TreeNode* pRoot, vector<int> &inorder_digui_re){ if (pRoot != NULL) { if (pRoot->left) inorder_digui(pRoot->left, inorder_digui_re); inorder_digui_re.push_back(pRoot->val); if (pRoot->right) inorder_digui(pRoot->right, inorder_digui_re); }}void inorder_while(TreeNode* pRoot, vector<int> &inorder_while_re){ stack<TreeNode*>stack1; while (pRoot || !stack1.empty()) { while (pRoot) { stack1.push(pRoot); pRoot = pRoot->left; } pRoot = stack1.top(); stack1.pop(); inorder_while_re.push_back(pRoot->val); pRoot = pRoot->right; }}void afterorder_digui(TreeNode* pRoot, vector<int> &afterorder_digui_re){ if (pRoot != NULL) { if (pRoot->left) afterorder_digui(pRoot->left, afterorder_digui_re); if (pRoot->right) afterorder_digui(pRoot->right, afterorder_digui_re); afterorder_digui_re.push_back(pRoot->val); }}void afterorder_while(TreeNode* pRoot, vector<int> &afterorder_while_re){ stack <TreeNode*>stack1; stack1.push(pRoot); stack1.push(pRoot); while (!stack1.empty()) { pRoot = stack1.top(); stack1.pop(); if (!stack1.empty() && stack1.top() == pRoot) { if (pRoot->right) { stack1.push(pRoot->right); stack1.push(pRoot->right); } if (pRoot->left) { stack1.push(pRoot->left); stack1.push(pRoot->left); } } else afterorder_while_re.push_back(pRoot->val); }}void kuandu_while(TreeNode *pRoot, vector<int>&kuandu_while_re){ queue<TreeNode*>queue1; queue1.push(pRoot); kuandu_while_re.push_back(pRoot->val); while (!queue1.empty()) { queue<TreeNode*>queue2; while (!queue1.empty()) { pRoot = queue1.front(); queue1.pop(); if (pRoot->left) { kuandu_while_re.push_back(pRoot->left->val); queue2.push(pRoot->left); } if (pRoot->right) { kuandu_while_re.push_back(pRoot->right->val); queue2.push(pRoot->right); } } queue1 = queue2; }}void kuandu_hang(TreeNode *pRoot, vector<vector<int> > &kuandu_hang_re){ queue<TreeNode*>queue1; queue1.push(pRoot); vector<int>re; re.push_back(pRoot->val); kuandu_hang_re.push_back(re); while (!queue1.empty()) { queue<TreeNode*>queue2; vector<int>re; while (!queue1.empty()) { pRoot = queue1.front(); queue1.pop(); if (pRoot->left) { re.push_back(pRoot->left->val); queue2.push(pRoot->left); } if (pRoot->right) { re.push_back(pRoot->right->val); queue2.push(pRoot->right); } } kuandu_hang_re.push_back(re); queue1 = queue2; }}
阅读全文
1 0
- 二叉树的七种遍历方式以及按行打印
- 二叉树的遍历以及按层打印
- 二叉树的建立以及三种遍历方式
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- C++编程练习(8)----“二叉树的建立以及二叉树的三种遍历方式“(前序遍历、中序遍历、后续遍历)
- 二叉树创建以及遍历方式
- 二叉树的遍历以及将二叉树倒置90°打印输入(递归)
- 二叉树的三种遍历方式
- 二叉树的3种遍历方式
- 二叉树的几种遍历方式
- 二叉树的三种遍历方式
- 二叉树的四种遍历方式
- 二叉树的三种遍历方式
- 二叉树的三种遍历方式
- 二叉树的三种遍历方式
- 二叉树的四种遍历方式
- 二叉树的几种遍历方式
- Java实现完全二叉树的构建以及三种遍历方式
- 集合工具类之List特点和实现类的详解
- EditorEvent
- 【bzoj1113】 [Poi2008]海报PLA
- 指纹识别分析之enroll流程
- linux下的EDA——DC使用
- 二叉树的七种遍历方式以及按行打印
- Gym
- 百练4982 踩方格
- linux复习之进程基础知识
- Contiki系统关于PROCESS_CONTEXT_BEGIN与PROCESS_CONTEXT_END见解
- H.264句法语义学习
- IO流与多线程
- 【bzoj3524】 [Poi2014]Couriers
- TCP四次挥手中的TIME_WAIT状态存在的理由