[LeetCode] Nonrecursive preorder traversal
2014-12-08 12:39
453 查看
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree
return
下面给出非递归前序遍历的算法,和非递归中序遍历非常像。
{1,#,2,3},
1 \ 2 / 3
return
[1,2,3].
下面给出非递归前序遍历的算法,和非递归中序遍历非常像。
vector<int> preorderTraversal(TreeNode *root) { vector<int> vec; // stores the preorder sequence stack<TreeNode*> st; // auxiliary stack TreeNode* curr = root; while(1) { while(curr) { vec.push_back(curr->val); st.push(curr); curr = curr->left; } if(st.size()==0) break; else { curr = st.top(); curr = curr->right; st.pop(); } } return vec; }
相关文章推荐
- [LeetCode] 非递归中序遍历二叉树 non recursive inorder traversal
- [LeetCode] Nonrecursive postorder traversal 非递归后续遍历
- JavaScript Lab - Articles - Non-recursive Preorder Traversal - Part 4
- JavaScript Lab - Articles - Non-recursive Preorder Traversal - Part 1
- [python]leetcode(105). Construct Binary Tree from Preorder and Inorder Traversal
- *LeetCode-Binary Tree Preorder Traversal
- LeetCode 之 Binary Tree Preorder Traversal(树)
- Construct Binary Tree from Preorder and Inorder Traversal ---LeetCode
- [LeetCode]题解(python):144-Binary Tree Preorder Traversal
- LeetCode144—Binary Tree Preorder Traversal
- LeetCode_144 Binary Tree Preorder Traversal
- LeetCode – Refresh – Binary Tree Pre Order Traversal
- leetcode_Binary Tree Preorder Traversal
- [Leetcode] Construct Binary Tree from Preorder and Inorder Traversal
- 【LeetCode】144 . Binary Tree Preorder Traversal 解题报告
- LeetCode题解:Binary Tree Preorder Traversal
- leetcode- Preorder/Inorder/PostOrder without Recursive
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode: Binary Tree Preorder Traversal