树的非递归遍历
来源:互联网 发布:java中的类命名规范 编辑:程序博客网 时间:2024/06/05 00:38
1.先序(我只把这些当模板)
class Solution {public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if(root==NULL)return res; stack<TreeNode*> ms; TreeNode* p=root; while(!ms.empty()||p) { if(p) { res.push_back(p->val); ms.push(p); p=p->left; } else if(!ms.empty()) { p=ms.top(); ms.pop(); if(p)p=p->right; } } return res; }};
2.中序
class Solution {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; if(root==NULL)return res; stack<TreeNode*> ms; TreeNode* p=root; while(!ms.empty()||p) { if(p) { ms.push(p); p=p->left; } else { p=ms.top(); res.push_back(p->val); ms.pop(); p=p->right; } } return res; }};
3.后序
class Solution {public: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; if(root==NULL)return res; stack<TreeNode*> ms; TreeNode *p=root,*pre=NULL; while(!ms.empty()||p) { if(p) { ms.push(p); p=p->left; } else { p=ms.top(); if(p->right!=NULL&&p->right!=pre)p=p->right; else { res.push_back(p->val); ms.pop(); pre=p; p=NULL; } } } return res; }};
看看大神的代码(以下代码来源于leetcode)
1.先序(看起来很优美)
class Solution {public:vector<int> preorderTraversal(TreeNode *root) { if (root==NULL) { return vector<int>(); } vector<int> result; stack<TreeNode *> treeStack; treeStack.push(root); while (!treeStack.empty()) { TreeNode *temp = treeStack.top(); result.push_back(temp->val); treeStack.pop(); if (temp->right!=NULL) { treeStack.push(temp->right); } if (temp->left!=NULL) { treeStack.push(temp->left); } } return result;}};
2.中序( o(1)空间复杂度)
Morris traversal
public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}public class Solution { public List<Integer> inorderTraversal(TreeNode root) { if(root == null) return new ArrayList<Integer>(); List<Integer> res = new ArrayList<Integer>(); TreeNode pre = null; while(root != null){ if(root.left == null){ res.add(root.val); root = root.right; }else{ pre = root.left; while(pre.right != null && pre.right != root){ pre = pre.right; } if(pre.right == null){ pre.right = root; root = root.left; }else{ pre.right = null; res.add(root.val); root = root.right; } } } return res; }}
3.后序(美得不敢直视)
vector<int> postorderTraversal(TreeNode *root) { vector<int> v; if (!root) return v; stack<TreeNode *> s; s.push(root); TreeNode *p = NULL; while(!s.empty()) { p = s.top(); s.pop(); v.insert(v.begin(), p->val); if (p->left) s.push(p->left); if (p->right) s.push(p->right); } return v;}
阅读全文
0 0
- 树的非递归遍历
- 树的非递归遍历
- 树的非递归遍历
- 树的遍历 非递归
- 树的非递归遍历
- 树的非递归遍历
- 树的非递归遍历
- 树的非递归遍历
- 树的非递归遍历
- 树的非递归遍历
- 树的非递归遍历
- 二叉树的先中后序遍历,递归遍历,非递归遍历
- 树的建立 递归非递归遍历
- 递归非递归实现树的遍历
- 二叉树的递归,非递归遍历
- 二叉树的递归+非递归遍历
- 树的遍历----递归与非递归
- 树的递归和非递归遍历
- 6.列表
- 学生、成绩、课程表,要求查询001课程比002课程成绩高的所有学生的学号解决办法
- 《剑指offer》-第2章(2)
- MySQL重建或修复表或索引
- 在windows上安装和启动Elasticseach
- 树的非递归遍历
- 在 Ubuntu 11.10 上安装 Sun Grid Engine
- C++入门基本知识
- Java常见的几种排序方法
- 函数式接口和lambda表达式
- C++map如何按值排序
- LeetCode 重建BST
- Python快速入门(5)-列表与字典推导式
- java.io.StreamCorruptedException: invalid type code: AC