二叉树的非递归中序遍历
来源:互联网 发布:bp神经网络java实现 编辑:程序博客网 时间:2024/05/05 06:44
输出二叉树的中序遍历序列,不允许使用递归
如[2,null,6,8],输出[2,8,6]
Binary Tree Inorder Traversal
借助栈,遍历二叉树
先让所有的左孩子结点入栈
再将右孩子结点入栈
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> inorderTraversal(TreeNode* root) { vector<int> result; stack<const TreeNode*> s; const TreeNode *cur=root; while(cur||!s.empty()) { if(cur) { s.push(cur); cur=cur->left; } else { cur=s.top(); s.pop(); result.push_back(cur->val); cur=cur->right; } } return result; }};
0 0
- 二叉树的中序递归,非递归遍历
- 二叉树非递归后序遍历(非递归遍历中最麻烦的一个)
- 【二叉树遍历】中序------非递归
- 非递归中序遍历二叉树
- 中序遍历二叉树 [非递归]
- 非递归中序遍历二叉树
- 非递归中序遍历二叉树
- 二叉树的中序遍历的非递归算法
- 非递归的二叉搜索树的中序遍历
- 前,中,后序遍历二叉树 (递归 && 非递归的栈 && 非递归非栈的线索二叉树)
- 中序遍历二叉树的非递归算法
- 二叉树的遍历(中序,非递归)
- 二叉树的中序遍历(非递归)
- 中序遍历二叉树的非递归形式
- 二叉树的非递归中序遍历
- 二叉树的中序遍历非递归
- 二叉树的中序遍历(非递归算法)
- 二叉树前后中序遍历的非递归实现
- Setting之蓝牙(1)
- 还是关于vcenter server
- 文章标题
- 【log】slf4j 使用手册
- 数据存取之Preference浅析
- 二叉树的非递归中序遍历
- Web前端黑客技术②——挖掘XSS漏洞
- CentOS---网络配置详解
- Ra
- reading environment variable python
- HDU 4283 You Are the One(区间dp)
- JAVA字符串格式化
- Iterator使用及ArrayList遍历注意事项
- 【封装】WriteToFile——数据写入文件