赞
踩
给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
示例 1:

输入:root = [1,null,2,3]
输出:[1,2,3]
示例 2:
输入:root = []
输出:[]
示例 3:
输入:root = [1]
输出:[1]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
- class Solution {
- public:
- void preorder(TreeNode *root, vector<int> &res) {
- if (root == nullptr) {
- return;
- }
- res.push_back(root->val);
- preorder(root->left, res);
- preorder(root->right, res);
- }
-
- vector<int> preorderTraversal(TreeNode *root) {
- vector<int> res;
- preorder(root, res);
- return res;
- }
- };
-
- 作者:LeetCode-Solution
- 链接:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/solution/er-cha-shu-de-qian-xu-bian-li-by-leetcode-solution/
- 来源:力扣(LeetCode)
- 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。