LeetCode Binary Tree Preorder Traversal 前序遍历二叉树 递归和非递归解法
来源:互联网 发布:网络弹窗广告 编辑:程序博客网 时间:2024/05/16 23:39
Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
2014-1-10 update:
先序遍历的非递归也是非常简单的:
vector<int> preorderTraversal(TreeNode *root) {vector<int> rs;if (!root) return rs;stack<TreeNode *> stk;stk.push(root);while (!stk.empty()){TreeNode *t = stk.top();stk.pop();rs.push_back(t->val);if (t->right) stk.push(t->right);if (t->left) stk.push(t->left);}return rs;}
的确如note说的一样,使用递归只需要几分钟就解决了,如果不是用递归,自己琢磨的话还是花了差不多半个小时。
看代码就知道非递归比递归长很多。
非递归方法主要难点:
1. 需要使用两个循环,左子树循环嵌套右子树循环;
2. 有右子树的时候,需要跳出本循环,让其重新进入左子树循环。
2. 无左子树的时候,需要出栈,而不是等到空子树才出栈。
下面使用两种方法解决一下这个题目,他们的时间效率都差不多,都是12ms,大概测试数据都很少,所以时间都一样。也是LeetCode上的一个水题了,测试数据都不多。
主函数都是一样的:
vector<int> preorderTraversal(TreeNode *root) {vector<int> prePath; preStack(root, prePath);return prePath;}
递归法:
void preNodes(TreeNode *node,vector<int> &onePath){if(node == nullptr) return;onePath.push_back(node->val);preNodes(node->left, onePath);preNodes(node->right, onePath);}
非递归法:
void preStack(TreeNode *node, vector<int> &prePath){if(node == nullptr) return;stack<TreeNode *> stk;TreeNode *cur = node;stk.push(cur);while (!stk.empty()){cur = stk.top();prePath.push_back(cur->val);while (cur->left){prePath.push_back(cur->left->val);cur = cur->left;stk.push(cur);}stk.pop();while (!stk.empty() || cur->right){if(cur->right){cur = cur->right;stk.push(cur);break;}else{cur = stk.top();stk.pop();}}//while}//while}
//2014-2-19 updatevector<int> preorderTraversal(TreeNode *root) {vector<int> rs;if (!root) return rs;stack<TreeNode *> stk;stk.push(root);while (!stk.empty()){TreeNode *t = stk.top();stk.pop();rs.push_back(t->val);if (t->right) stk.push(t->right);if (t->left) stk.push(t->left);}return rs;}
- LeetCode Binary Tree Preorder Traversal 前序遍历二叉树 递归和非递归解法
- LeetCode:Binary Tree Preorder Traversal(非递归方法前序遍历二叉树)
- 【LeetCode-面试算法经典-Java实现】【144-Binary Tree Preorder Traversal(二叉树非递归前序遍历)】
- LintCode Binary Tree Preorder Traversal二叉树的前序遍历(非递归)
- LeetCode Binary Tree Preorder Traversal (二叉树先序遍历非递归)
- 【leetcode】非递归先序遍历二叉树(Binary Tree Preorder Traversal)
- leetcode 144. Binary Tree Preorder Traversal-前序遍历|递归|非递归
- LeetCode Binary Tree Postorder Traversal 二叉树后序遍历 递归和非递归解法
- [leetcode] Binary Tree Preorder Traversal 非递归先序遍历
- LeetCode 144. Binary Tree Preorder Traversal (前序遍历的非递归实现)
- 【LeetCode】Binary Tree Preorder Traversal 二叉树前序遍历递归以及非递归算法
- Binary Tree Preorder Traversal 二叉树的先序遍历,使用堆栈,非递归
- LeetCode:Binary Tree Traversal(二叉树遍历非递归)
- 【LeetCode】144. Binary Tree Preorder Traversal 二叉树先序遍历的非递归实现
- LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
- 二叉树前/中/后序遍历非递归实现(用栈实现)LeetCode Binary Tree Traversal
- Binary Tree Preorder Traversal 二叉树的前向遍历(迭代非递归版本)
- Binary Tree Inorder Traversal 中序遍历二叉树 递归和非递归版
- C语言中指针的几点注意
- 2013-11-07 实验之火柴游戏(递归思维逻辑,转化思维,查找最优解思维)
- InternetOpen/InternetOpenUrl/InternetReadFile 等相关Win32 网络API 使用详细说明
- 为什么要使用NoSQL
- asp中rs.addnew与rs.update更新数据库的方法
- LeetCode Binary Tree Preorder Traversal 前序遍历二叉树 递归和非递归解法
- 最近在做ASP.net网站用到的技术和遇到的问题做个记录
- swing中setSize与setPreferredsize的区别
- Linux Shell脚本学习
- 第11周项目一--调用函数输出星号图(2)
- 给超链接加onclick事件
- 求二个数的最大公约数
- 单机配置MYSQL多实例
- 部署iwebshop,ecshop到Linux服务器