-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path102.cpp
More file actions
28 lines (27 loc) · 719 Bytes
/
Copy path102.cpp
File metadata and controls
28 lines (27 loc) · 719 Bytes
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
28
// preOrderReversion-3ms.cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
void preOrder(int i, TreeNode* root, vector<vector<int>>& nums) {
if (root == nullptr)
return;
if (nums.size() == i)
nums.push_back({});
nums[i].push_back(root->val);
preOrder(i + 1, root->left, nums);
preOrder(i + 1, root->right, nums);
}
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
preOrder(0, root, result);
return result;
}
};