94. Binary Tree Inorder Traversal&144. Binary Tree Preorder Traversal&145. Binary Tree Postorder Tra
来源:互联网 发布:入驻淘宝的费用是多少 编辑:程序博客网 时间:2024/05/04 21:59
二叉树的前中后序遍历。
前序:读取根节点的值并压栈,一直向左找到最左的节点后返回上一层开始向右寻找。
中序:将根节点一直压栈并向左寻找,找到最左的节点后读取该节点的值并返回上一层开始向右寻找。
这两个其实就是读取值的时间有点不一样而已。
后序:后序麻烦一点,输出顺序是左右中,可以通过变量记录当前节点的子树是否已经输出过,一个技巧性的方法是可以设置一个变量记录之前被输出的结点,利用后序遍历父节点的输出一定挨着它的子节点的这一性质,如果之前被输出的结点不是当前节点的右子树且右子树非空的话访问节点右子树,否则输出父节点。
前序:
class Solution {public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; TreeNode* p=root; stack<TreeNode* > s; while(!s.empty()||p){ if(p){ res.push_back(p->val); s.push(p); p=p->left; } else{ p=s.top(); s.pop(); p=p->right; } } return res; }};
中序:
class Solution {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> res; stack<TreeNode*> tree; TreeNode* p=root; while(!tree.empty()||p){ if(p){ tree.push(p); p=p->left; } else{ p=tree.top(); res.push_back(p->val); tree.pop(); p=p->right; } } return res; }};
后序:
class Solution {public: vector<int> postorderTraversal(TreeNode* root) { vector<int> res; stack<TreeNode*> s; TreeNode* p=root, *LastVisit=NULL; while(true){ while(p){ s.push(p); p=p->left; } if(s.empty()) break; p=s.top(); if(p->right&&p->right!=LastVisit) p=p->right; else{ s.pop(); res.push_back(p->val); LastVisit=p; p=NULL; } } return res; }};
0 0
- 94. Binary Tree Inorder Traversal&144. Binary Tree Preorder Traversal&145. Binary Tree Postorder Tra
- 94. Binary Tree Inorder Traversal\144. Binary Tree Preorder Traversal\145. Binary Tree Postorder
- 144. Binary Tree Preorder Traversal && 145. Binary Tree Postorder Traversal
- 144. Binary Tree Preorder Traversal &&94. Binary Tree Inorder Traversal
- 【LeetCode】Binary Tree Preorder &Inorder & Postorder Traversal
- LeeCode Binary Tree Preorder ,inorder ,postorder Traversal
- LeetCode--Binary Tree Traversal ( Inorder, Preorder, PostOrder)
- Binary Tree Traversal(Preorder, Inorder, Postorder )
- Binary Tree Inorder/Preorder/Postorder Traversal(Java)
- Binary Tree Preorder Traversal & Binary Tree Inorder Traversal & Binary Tree Postorder Traversal
- Binary Tree Preorder/postorder Traversal
- leetcode Binary Tree Preorder Traversal Binary Tree Postorder Traversal Inorder Traversal
- Binary Tree Preorder Traversal & Postorder Traversal
- Binary Tree Inorder Traversal & Preorder Traversal
- Binary Tree Preorder Traversal & Binary Tree Postorder Traversal
- [LeetCode] Binary Tree Preorder Traversal & Binary Tree Postorder Traversal
- [leetcode]Binary Tree Postorder Traversal&&Binary Tree Preorder Traversal
- LeetCode-Binary Tree Postorder Traversal&Binary Tree Preorder Traversal
- vs生成的文件如何自动拷贝到指定目录
- Linux 下软件安装
- MAC OX 配置JDK环境变量
- javascript中的继承与深度拷贝
- 欢迎使用CSDN-markdown编辑器
- 94. Binary Tree Inorder Traversal&144. Binary Tree Preorder Traversal&145. Binary Tree Postorder Tra
- WebGL数据制作流程及加载
- JS判断客户端是否是iOS或者Android
- NGUI 播放生音
- Android手势监测-GestureDector
- JavaScript权威指南_174_第17章_事件处理_17.8-文本事件
- 网友USB枚举过程分析
- 对话Linus Torvalds:大多黑客甚至连指针都未理解
- Ubuntu系统学习笔记-7.Tomcat下设置https的一个小问题