[LeetCode] 非递归中序遍历二叉树 non recursive inorder traversal
来源:互联网 发布:淘宝卖家漏发货怎么办 编辑:程序博客网 时间:2024/06/06 16:21
非递归中序遍历二叉树 non recursive inorder traversal
非递归前序遍历,参看这里。
用两个 while 循环来做, 思路如下.
1) Create an empty stack S.2) Initialize current node as root3) Push the current node to S and set current = current->left until current is NULL4) If current is NULL and stack is not empty then a) Pop the top item from stack. b) Print the popped item, set current = popped_item->right c) Go to step 3.5) If current is NULL and stack is empty then we are done.
下面的代码经过简单修改,可以完成逆中序遍历,即先访问右子树,再访问根节点,最后访问左子树。
vector<int> inorderTraversal(TreeNode *root) { vector<int> vec; // stores the preorder sequence stack<TreeNode*> st; // auxiliary stack TreeNode* curr = root; while(1) { while(curr) { st.push(curr); curr = curr->left; } if(st.size()==0) break; else { curr = st.top(); st.pop(); vec.push_back(curr->val); curr = curr->right; } } return vec; }
上面的代码和非递归前序遍历几乎一样。
0 0
- [LeetCode] 非递归中序遍历二叉树 non recursive inorder traversal
- 【leetcode】94Binary Tree Inorder Traversal(非递归中序遍历二叉树)
- Binary Tree Inorder Traversal 中序遍历二叉树 递归和非递归版
- LintCode Binary Tree Inorder Traversal 二叉树的中序遍历(非递归)
- LeetCode Binary Tree Inorder Traversal 中序遍历二叉树
- leetcode 94. Binary Tree Inorder Traversal-中序遍历|递归|非递归
- leetcode_94. Binary Tree Inorder Traversal 二叉树的中序遍历,非递归算法,栈的思想
- 【LeetCode】Binary Tree Inorder Traversal 二叉树中序遍历递归以及非递归算法
- LeetCode 94. Binary Tree Inorder Traversal 树的前序、中序,后序遍历的非递归实现
- Leetcode 94:Binary Tree Inorder Traversal (二叉树中序遍历,非递归)
- 94.LeetCode Binary Tree Inorder Traversal(medium)[中序遍历二叉树 树的遍历]
- Binary Tree Inorder Traversal 二叉树的中序遍历(迭代非递归版本)
- LeetCode:Binary Tree Traversal(二叉树遍历非递归)
- leetcode 94. Binary Tree Inorder Traversal 二叉树中序递归遍历
- LeetCode Binary Tree Preorder Traversal 前序遍历二叉树 递归和非递归解法
- LeetCode | Binary Tree Inorder Traversal(二叉树的中序遍历)
- LeetCode—***Binary Tree Inorder Traversal二叉树的中序遍历
- LeetCode OJ 之 Binary Tree Inorder Traversal (二叉树的中序遍历)
- Android 开发之旅:view的几种布局方式及实践
- 滋籽啄赘撞仔追桌专咨赘撞仔追桌
- 桶排序和样本排序(Bucket Sort and Sample Sort)
- 卓综撰足拽渍壮族追总足拽渍壮族
- 族揍自奏卓捉柞昨谞纂奏卓捉柞昨
- [LeetCode] 非递归中序遍历二叉树 non recursive inorder traversal
- 《c++ primer》第6章 函数 笔记
- GEC210(S5PV210)uboot引导内核配置
- 线程绑定CPU核
- 设计模式之适配器模式(Adapter)
- 黑马程序员--Java基础之二叉树数据结构和泛型
- Java线程池使用说明
- liferay中actionURL和renderURL的差别
- 判断两个线段相交