Skip to content

Commit 173b314

Browse files
easy LNR concept..see once
Given the root of a binary tree, return the inorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Example 2: Input: root = [] Output: [] Example 3: Input: root = [1] Output: [1] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 <= Node.val <= 100 Follow up: Recursive solution is trivial, could you do it iteratively?
1 parent 8f996c5 commit 173b314

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

binary-tree-inorder-traversal.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
15+
void inOrder(TreeNode* root,vector<int> &ans){
16+
if(root==nullptr){
17+
return;
18+
}
19+
inOrder(root->left,ans);
20+
ans.push_back(root->val);
21+
inOrder(root->right,ans);
22+
}
23+
vector<int> inorderTraversal(TreeNode* root) {
24+
vector<int> ans;
25+
inOrder(root,ans);
26+
return ans;
27+
}
28+
};
29+

0 commit comments

Comments
 (0)