一、题目描述
给定一个二叉树,返回它的前序遍历结果。
例如输入二叉树 [1,null,2,3]:
|
1 2 3 4 5 |
1 \ 2 / 3 |
输出:
|
1 |
[1,2,3] |
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
二、题解
二叉树的前序遍历,递归和非递归方式。
参考:二叉树的前序遍历。
三、代码
3.1 递归代码
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Solution { void preorderTraversal(TreeNode *root, vector<int> &v) { if (root == nullptr) { return; } v.push_back(root->val); if (root->left) { preorderTraversal(root->left, v); } if (root->right) { preorderTraversal(root->right, v); } } public: vector<int> preorderTraversal(TreeNode* root) { vector<int> v; preorderTraversal(root, v); return v; } }; |

3.2 非递归代码
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Solution { public: vector<int> preorderTraversal(TreeNode* root) { stack<TreeNode *> st; TreeNode *p; vector<int> v; p = root; while (p || !st.empty()) { while (p) { // 遍历当前节点 v.push_back(p->val); st.push(p); // 遍历左子树 p = p->left; } // 通过栈来遍历右子树 if (!st.empty()) { p = st.top()->right; st.pop(); } } return v; } }; |

![[leetcode]556-下一个更大的元素III](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/7119d-image968307faea0b8cac.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]-409最长回文子串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/e5589-image788d511d7c1e839c.png&w=280&h=210&a=&zc=1)
![[leetcode]44-通配符匹配](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/9d6a3-image4b1dccbeba2a0cc0.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode+剑指offer] 169-多数元素](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/d3450-image.png&w=280&h=210&a=&zc=1)

![[leetcode]199-二叉树的右视图](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/f24a8-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]72-编辑距离](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/88c60-imageff84a6c5047db6ed.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]460-LFU缓存](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/5b3f2-image.png&w=280&h=210&a=&zc=1)
![[leetcode]125-验证回文串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/5bfa5-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[程序员面试宝典]17.16-按摩师](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/9ecc9-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]876-链表的中间节点](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/64d0d-imagece778ab033c9d90d.png&w=280&h=210&a=&zc=1)
![【每日打卡】[剑指offer]面试题40-最小的k个数](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/c83c9-imagec745c425c670e18c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]面试题1.6-字符串压缩](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/776bc-image.png&w=280&h=210&a=&zc=1)
评论