Skip to content

Commit 86a046d

Browse files
committed
Add 5 solutions for binary tree traversal problems
- Added solutions for preorder and postorder traversal of binary trees. - Added solutions for preorder and postorder traversal of n-ary trees. - Added solution for inorder traversal of binary trees.
1 parent c215ed2 commit 86a046d

File tree

15 files changed

+563
-0
lines changed

15 files changed

+563
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Binary Tree Preorder Traversal
2+
3+
**Question ID**: 144
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 0 ms
7+
**Memory**: 11.00 MB
8+
9+
## Solution Code
10+
11+
```cpp
12+
//https://leetcode.com/problems/binary-tree-preorder-traversal
13+
/**
14+
* Definition for a binary tree node.
15+
* struct TreeNode {
16+
* int val;
17+
* TreeNode *left;
18+
* TreeNode *right;
19+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
20+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
21+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
22+
* };
23+
*/
24+
class Solution {
25+
public:
26+
vector<int> order;
27+
vector<int> preorderTraversal(TreeNode* root) {
28+
29+
order.clear();
30+
Traversal(root);
31+
32+
return order;
33+
}
34+
35+
void Traversal(TreeNode* node){
36+
if(node==nullptr)
37+
return;
38+
39+
order.push_back(node->val);
40+
41+
if(node->left!=nullptr)
42+
Traversal(node->left);
43+
44+
if(node->right!=nullptr)
45+
Traversal(node->right);
46+
47+
}
48+
};
49+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1488359612,
3+
"question_id": 144,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1735166931,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "0 ms",
10+
"url": "https://leetcode.com/submissions/detail/1488359612/",
11+
"is_pending": "Not Pending",
12+
"title": "Binary Tree Preorder Traversal",
13+
"memory": "11.00 MB",
14+
"title_slug": "binary-tree-preorder-traversal",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// https://leetcode.com/problems/binary-tree-preorder-traversal
5+
/**
6+
* Definition for a binary tree node.
7+
* struct TreeNode {
8+
* int val;
9+
* TreeNode *left;
10+
* TreeNode *right;
11+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
13+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
14+
* };
15+
*/
16+
class Solution
17+
{
18+
public:
19+
vector<int> order;
20+
vector<int> preorderTraversal(TreeNode *root)
21+
{
22+
23+
order.clear();
24+
Traversal(root);
25+
26+
return order;
27+
}
28+
29+
void Traversal(TreeNode *node)
30+
{
31+
if (node == nullptr)
32+
return;
33+
34+
order.push_back(node->val);
35+
36+
if (node->left != nullptr)
37+
Traversal(node->left);
38+
39+
if (node->right != nullptr)
40+
Traversal(node->right);
41+
}
42+
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Binary Tree Postorder Traversal
2+
3+
**Question ID**: 145
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 0 ms
7+
**Memory**: 11.18 MB
8+
9+
## Solution Code
10+
```cpp
11+
//https://leetcode.com/problems/binary-tree-postorder-traversal
12+
/**
13+
* Definition for a binary tree node.
14+
* struct TreeNode {
15+
* int val;
16+
* TreeNode *left;
17+
* TreeNode *right;
18+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
19+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
20+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
21+
* };
22+
*/
23+
class Solution {
24+
public:
25+
26+
vector<int> order;
27+
vector<int> postorderTraversal(TreeNode* root) {
28+
order.clear();
29+
30+
Traversal(root);
31+
32+
return order;
33+
}
34+
35+
void Traversal(TreeNode* node){
36+
if(node==nullptr)
37+
return;
38+
39+
if(node->left!=nullptr)
40+
Traversal(node->left);
41+
42+
43+
if(node->right!=nullptr)
44+
Traversal(node->right);
45+
46+
order.push_back(node->val);
47+
}
48+
};
49+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1488362191,
3+
"question_id": 145,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1735167261,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "0 ms",
10+
"url": "https://leetcode.com/submissions/detail/1488362191/",
11+
"is_pending": "Not Pending",
12+
"title": "Binary Tree Postorder Traversal",
13+
"memory": "11.18 MB",
14+
"title_slug": "binary-tree-postorder-traversal",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// https://leetcode.com/problems/binary-tree-postorder-traversal
5+
6+
/**
7+
* Definition for a binary tree node.
8+
* struct TreeNode {
9+
* int val;
10+
* TreeNode *left;
11+
* TreeNode *right;
12+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
13+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
14+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
15+
* };
16+
*/
17+
class Solution
18+
{
19+
public:
20+
vector<int> order;
21+
vector<int> postorderTraversal(TreeNode *root)
22+
{
23+
order.clear();
24+
25+
Traversal(root);
26+
27+
return order;
28+
}
29+
30+
void Traversal(TreeNode *node)
31+
{
32+
if (node == nullptr)
33+
return;
34+
35+
if (node->left != nullptr)
36+
Traversal(node->left);
37+
38+
if (node->right != nullptr)
39+
Traversal(node->right);
40+
41+
order.push_back(node->val);
42+
}
43+
};
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# N-ary Tree Preorder Traversal
2+
3+
**Question ID**: 589
4+
**Language**: C++
5+
**Status**: Accepted
6+
**Runtime**: 17 ms
7+
**Memory**: 15.74 MB
8+
9+
## Solution Code
10+
11+
```cpp
12+
//https://leetcode.com/problems/n-ary-tree-preorder-traversal
13+
/*
14+
// Definition for a Node.
15+
class Node {
16+
public:
17+
int val;
18+
vector<Node*> children;
19+
20+
Node() {}
21+
22+
Node(int _val) {
23+
val = _val;
24+
}
25+
26+
Node(int _val, vector<Node*> _children) {
27+
val = _val;
28+
children = _children;
29+
}
30+
};
31+
*/
32+
33+
class Solution {
34+
public:
35+
vector<int> order;
36+
vector<int> preorder(Node* root) {
37+
order.clear();
38+
39+
Traversal(root);
40+
41+
return order;
42+
}
43+
44+
void Traversal(Node* node){
45+
if(node==nullptr)
46+
return;
47+
48+
order.push_back(node->val);
49+
50+
for(auto n : node->children)
51+
Traversal(n);
52+
53+
}
54+
};
55+
```
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": 1488361003,
3+
"question_id": 589,
4+
"lang": "cpp",
5+
"lang_name": "C++",
6+
"timestamp": 1735167057,
7+
"status": 10,
8+
"status_display": "Accepted",
9+
"runtime": "17 ms",
10+
"url": "https://leetcode.com/submissions/detail/1488361003/",
11+
"is_pending": "Not Pending",
12+
"title": "N-ary Tree Preorder Traversal",
13+
"memory": "15.74 MB",
14+
"title_slug": "n-ary-tree-preorder-traversal",
15+
"has_notes": false,
16+
"flag_type": 1
17+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// https://leetcode.com/problems/n-ary-tree-preorder-traversal
5+
6+
/*
7+
// Definition for a Node.
8+
class Node {
9+
public:
10+
int val;
11+
vector<Node*> children;
12+
13+
Node() {}
14+
15+
Node(int _val) {
16+
val = _val;
17+
}
18+
19+
Node(int _val, vector<Node*> _children) {
20+
val = _val;
21+
children = _children;
22+
}
23+
};
24+
*/
25+
26+
class Solution
27+
{
28+
public:
29+
vector<int> order;
30+
vector<int> preorder(Node *root)
31+
{
32+
order.clear();
33+
34+
Traversal(root);
35+
36+
return order;
37+
}
38+
39+
void Traversal(Node *node)
40+
{
41+
if (node == nullptr)
42+
return;
43+
44+
order.push_back(node->val);
45+
46+
for (auto n : node->children)
47+
Traversal(n);
48+
}
49+
};

0 commit comments

Comments
 (0)