Binary Tree Inorder Traversal
来源:互联网 发布:linux u盘启动盘 编辑:程序博客网 时间:2024/04/24 09:32
问题:二叉树的非递归中序遍历。
//code
class Solution {public: vector<int> inorderTraversal(TreeNode *root) { stack<TreeNode *> s; vector<int> vec; if(root == NULL) return vec; s.push(root); while(!s.empty()) { TreeNode *cur = s.top(); while(cur->left) {s.push(cur->left); cur = cur->left;} while(cur->right == NULL) { vec.push_back(cur->val); s.pop(); if(!s.empty()) cur = s.top(); else break; } if(cur->right) { vec.push_back(cur->val); s.pop(); s.push(cur->right); } } return vec; }};
程序直接在线写的,格式不是很好,见谅。
0 0
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal
- 正则表达式逗号分隔 (c++)
- C++基础学习笔记(1):C++中的操作符
- openfire+asmock实现消息推送
- DB2必须了解的常用命令及技巧(转)
- 长按鼠标事件js
- Binary Tree Inorder Traversal
- PHP array_pad() 函数
- 【转载】《MySQL入门很简单》学习笔记
- 在iOS中创建静态库
- Git 的origin和master分析
- IK Analyzer
- Servlet API中的主要接口与类的作用和生命周期
- android activity加载布局文件后,组件为空java.lang.NullPointerException
- windows驱动编程-串口调试(VMWare虚拟机下wi7 和 Virtual Box下xp)