-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path94_Binary_Tree_Inorder_Traversal.cpp
More file actions
44 lines (37 loc) 路 1003 Bytes
/
Copy path94_Binary_Tree_Inorder_Traversal.cpp
File metadata and controls
44 lines (37 loc) 路 1003 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Given a Binary Tree, find the In-Order Traversal of it.
Example 1:
Input:
1
/ \
3 2
Output: 3 1 2
Example 2:
Input:
10
/ \
20 30
/ \ /
40 60 50
Output: 40 20 60 10 50 30
Your Task:
You don't need to read input or print anything. Your task is to complete the function inOrder() that takes root node of the tree as input and returns a list containing the In-Order Traversal of the given Binary Tree.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= Number of nodes <= 105
0 <= Data of a node <= 105
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ans;
if (root == NULL) return ans;
vector<int> left = inorderTraversal(root->left);
ans.insert(ans.end(), left.begin(), left.end());
ans.push_back(root->val);
vector<int> right = inorderTraversal(root->right);
ans.insert(ans.end(), right.begin(), right.end());
return ans;
}
};