树的遍历:非递归形式
来源:互联网 发布:python抓取图片 编辑:程序博客网 时间:2024/06/11 02:12
转自Leetcode论坛中一道关于树遍历题目的讨论:https://oj.leetcode.com/discuss/9736/accepted-code-with-explaination-does-anyone-have-better-idea
There is an universal idea for preorder traversal inorder traversal and postorder traversal. For each node N, we process it as follows:------- push N in stack -> push left tree of N in stack -> pop left tree of N -> push right tree of N in stack -> pop right tree of N -> pop N--------- For preorder traversal, we visit a node when pushing it in stack. For inorder traversal, we visit a node when pushing its right child in stack. for postorder traversal, we visit a node when popping it. lastpop represents the node which is popped the last time. For the top node in stack, it has three choices, pushing its left child in stack, or pushing its right child in stack, or being popped. If lastpop != top->left, meaning that its left tree has not been pushed in stack, then we push its left child. If last_pop == top->left, we push its right child. Otherwise, we pop the top node.
void preorder_traversal_iteratively(TreeNode* root){ if (root == 0) return; stack<TreeNode*> s; s.push(root); cout << root->val << ' '; // visit root TreeNode* last_pop = root; while (!s.empty()) { TreeNode* top = s.top(); if (top->left != 0 && top->left != last_pop && top->right != last_pop) // push_left { s.push(top->left); cout << top->left->val << ' '; // visit top->left } else if (top->right != 0 && top->right != last_pop && (top->left == 0 || top->left == last_pop)) // push_right { s.push(top->right); cout << top->right->val << ' '; // visit top->right } else // pop { s.pop(); last_pop = top; } }}void inorder_traversal_iteratively(TreeNode* root){ if (root == 0) return; stack<TreeNode*> s; s.push(root); TreeNode* last_pop = root; while (!s.empty()) { TreeNode* top = s.top(); if (top->left != 0 && top->left != last_pop && top->right != last_pop) // push_left { s.push(top->left); } else if (top->right != 0 && top->right != last_pop && (top->left == 0 || top->left == last_pop)) // push_right { s.push(top->right); cout << top->val << ' '; // visit top } else // pop { s.pop(); last_pop = top; if (top->right == 0) cout << top->val << ' '; // visit top } }}void postorder_traversal_iteratively(TreeNode* root){ if (root == 0) return; stack<TreeNode*> s; s.push(root); TreeNode* last_pop = root; while (!s.empty()) { TreeNode* top = s.top(); if (top->left != 0 && top->left != last_pop && top->right != last_pop) // push_left { s.push(top->left); } else if (top->right != 0 && top->right != last_pop && (top->left == 0 || top->left == last_pop)) // push_right { s.push(top->right); } else // pop { s.pop(); last_pop = top; cout << top->val << ' '; // visit top } }}
- 二叉树的遍历非递归形式
- 树的遍历:非递归形式
- 二叉树的遍历(非递归形式)
- 中序遍历二叉树的非递归形式
- 二叉树的非递归统一遍历形式
- 二叉树的前序遍历非递归形式
- 二叉树三种遍历的非递归形式
- 二叉树前序中序后序遍历的非递归形式
- 用递归和非递归的形式实现二叉树的前中后序遍历
- 二叉树的前序、中序、后序遍历的算法(递归形式和非递归形式)
- 树的非递归遍历
- 树的非递归遍历
- 树的非递归遍历
- 树的遍历 非递归
- 树的非递归遍历
- 树的非递归遍历
- 树的非递归遍历
- 树的非递归遍历
- 信息安全会
- 基于AWS的远程灾备系统架构
- 单元测试军规:如何编写更好的单元测试
- HDU1166-线段树模版-敌兵布阵
- 水银体温计摔坏了怎样处理---与开发无关的生活技巧
- 树的遍历:非递归形式
- hadoop 客户端配置问题备忘----持续添加
- 试读《C#高级编程(第9版)》有感
- servlet3.0规范异步请求
- 【SICP练习】5 练习1.9
- 进制转化
- eclipse 一直 android sdk contentloader
- spring多数据源+事物管理
- 将数据类型牢记在❤