Skip to content

Commit 7cb29cf

Browse files
authored
Merge pull request #1971 from parikhitritgithub/dev3
Adding preorder, postorder, and inorder traversal techniques to the Tree section
2 parents 855f262 + 5b81bed commit 7cb29cf

File tree

5 files changed

+553
-0
lines changed

5 files changed

+553
-0
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
---
2+
id: in-order-traversal
3+
title: in-order-traversal
4+
sidebar_label: In Order Traversal
5+
tags:
6+
- Tree
7+
- Depth-First Search
8+
- Binary Tree
9+
description: "This is a solution to the Binary Tree In Order Traversal problem on LeetCode."
10+
---
11+
12+
## Problem Description
13+
Given the root of a binary tree, return the inorder traversal of its nodes' values.
14+
15+
### Examples
16+
17+
**Example 1:**
18+
![alt text](inorder_1-1.jpg)
19+
```
20+
Input: root = [1,null,2,3]
21+
Output: [1,2,3]
22+
```
23+
24+
**Example 2:**
25+
```
26+
Input: root = []
27+
Output: []
28+
```
29+
30+
**Example 3:**
31+
```
32+
Input: root = [1]
33+
Output: [1]
34+
```
35+
36+
37+
38+
### Constraints
39+
- The number of nodes in the tree is in the range [0, 100].
40+
- `-1000 <= Node.val <= 1000`
41+
42+
## Solution for Binary Tree Pre Order Traversal
43+
44+
### Intuition
45+
- Recursive is very easy try with iterative way
46+
47+
### Code in Different Languages
48+
49+
50+
<Tabs>
51+
<TabItem value="Python" label="Python">
52+
<SolutionAuthor name="@parikhitkurmi"/>
53+
54+
```python
55+
//python
56+
57+
# Definition for a binary tree node.
58+
# class TreeNode:
59+
# def __init__(self, val=0, left=None, right=None):
60+
# self.val = val
61+
# self.left = left
62+
# self.right = right
63+
class Solution:
64+
def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
65+
66+
def InOrder(root,arr):
67+
68+
if root is None:
69+
return
70+
71+
else:
72+
InOrder(root.left,arr)
73+
arr.append(root.val)
74+
InOrder(root.right,arr)
75+
76+
return arr
77+
78+
return InOrder(root,[])
79+
80+
81+
```
82+
</TabItem>
83+
<TabItem value="Java" label="Java">
84+
<SolutionAuthor name="@parikhitkurmi"/>
85+
86+
```java
87+
//java
88+
89+
90+
/**
91+
* Definition for a binary tree node.
92+
* public class TreeNode {
93+
* int val;
94+
* TreeNode left;
95+
* TreeNode right;
96+
* TreeNode() {}
97+
* TreeNode(int val) { this.val = val; }
98+
* TreeNode(int val, TreeNode left, TreeNode right) {
99+
* this.val = val;
100+
* this.left = left;
101+
* this.right = right;
102+
* }
103+
* }
104+
*/
105+
class Solution {
106+
public List<Integer> inorderTraversal(TreeNode root) {
107+
108+
List<Integer> ans = new ArrayList<>();
109+
ArrayDeque<TreeNode> stack = new ArrayDeque<>();
110+
111+
TreeNode cur = root;
112+
113+
while(cur != null || !stack.isEmpty()) {
114+
115+
while(cur != null) {
116+
117+
stack.push(cur);
118+
cur = cur.left;
119+
}
120+
121+
TreeNode pop = stack.pop();
122+
ans.add(pop.val);
123+
124+
cur = pop.right;
125+
}
126+
return ans;
127+
}
128+
}
129+
130+
131+
132+
133+
```
134+
</TabItem>
135+
<TabItem value="C++" label="C++">
136+
<SolutionAuthor name="@parikhitkurmi"/>
137+
138+
```cpp
139+
//cpp
140+
141+
/**
142+
* Definition for a binary tree node.
143+
* struct TreeNode {
144+
* int val;
145+
* TreeNode *left;
146+
* TreeNode *right;
147+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
148+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
149+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
150+
* };
151+
*/
152+
class Solution {
153+
public:
154+
155+
vector<int> ans ;
156+
void inorder (TreeNode* root) {
157+
158+
// inorder(LNR)
159+
160+
if(!root)
161+
return ;
162+
163+
164+
165+
inorder(root->left) ;
166+
167+
168+
ans.push_back(root->val);
169+
170+
171+
inorder(root->right) ;
172+
173+
174+
}
175+
vector<int> inorderTraversal(TreeNode* root) {
176+
177+
inorder(root) ;
178+
179+
return ans ;
180+
181+
}
182+
};
183+
184+
```
185+
186+
</TabItem>
187+
</Tabs>
188+
189+
190+
191+
192+
193+
## References
194+
195+
- **LeetCode Problem:** [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/)
196+
- **Solution Link:** [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/submissions/)
197+
- **Authors GeeksforGeeks Profile:** [parikhit kurmi](https://www.geeksforgeeks.org/user/sololeveler673/)
198+
- **Authors Leetcode:** [parikhit kurmi](https://leetcode.com/u/parikhitkurmi14/)
199+

dsa/Algorithms/Tree/inorder_1-1.jpg

8.45 KB
Loading

dsa/Algorithms/Tree/inorder_1.jpg

8.45 KB
Loading
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
id: post-order-traversal
3+
title: post-order-traversal
4+
sidebar_label: Post Order Traversal
5+
tags:
6+
- Tree
7+
- Depth-First Search
8+
- Binary Tree
9+
description: "This is a solution to the Binary Tree Post Order Traversal problem on LeetCode."
10+
---
11+
12+
## Problem Description
13+
Given the root of a binary tree, return the postorder traversal of its nodes' values.
14+
15+
### Examples
16+
17+
**Example 1:**
18+
![alt text](inorder_1-1.jpg)
19+
```
20+
Input: root = [1,null,2,3]
21+
Output: [1,2,3]
22+
```
23+
24+
**Example 2:**
25+
```
26+
Input: root = []
27+
Output: []
28+
```
29+
30+
**Example 3:**
31+
```
32+
Input: root = [1]
33+
Output: [1]
34+
```
35+
36+
37+
38+
### Constraints
39+
- The number of nodes in the tree is in the range [0, 100].
40+
- `-1000 <= Node.val <= 1000`
41+
42+
## Solution for Binary Tree Post Order Traversal
43+
44+
### Intuition
45+
- Recursive is very easy try with iterative way
46+
47+
### Code in Different Languages
48+
49+
50+
<Tabs>
51+
<TabItem value="Python" label="Python">
52+
<SolutionAuthor name="@parikhitkurmi"/>
53+
54+
```python
55+
//python
56+
# Definition for a binary tree node.
57+
# class TreeNode(object):
58+
# def __init__(self, val=0, left=None, right=None):
59+
# self.val = val
60+
# self.left = left
61+
# self.right = right
62+
class Solution(object):
63+
def postorderTraversal(self, root):
64+
"""
65+
:type root: TreeNode
66+
:rtype: List[int]
67+
"""
68+
stack=[]
69+
def dfs(root):
70+
if root:
71+
dfs(root.left)
72+
dfs(root.right)
73+
stack.append(root.val)
74+
dfs(root)
75+
return stack
76+
77+
78+
```
79+
</TabItem>
80+
<TabItem value="Java" label="Java">
81+
<SolutionAuthor name="@parikhitkurmi"/>
82+
83+
```java
84+
//java
85+
86+
/**
87+
* Definition for a binary tree node.
88+
* public class TreeNode {
89+
* int val;
90+
* TreeNode left;
91+
* TreeNode right;
92+
* TreeNode() {}
93+
* TreeNode(int val) { this.val = val; }
94+
* TreeNode(int val, TreeNode left, TreeNode right) {
95+
* this.val = val;
96+
* this.left = left;
97+
* this.right = right;
98+
* }
99+
* }
100+
*/
101+
class Solution {
102+
ArrayList<Integer> res = new ArrayList<>();
103+
public List<Integer> postorderTraversal(TreeNode root) {
104+
postOrder(root);
105+
return res;
106+
}
107+
108+
private void postOrder(TreeNode root){
109+
if(root == null){
110+
return;
111+
}
112+
postOrder(root.left);
113+
postOrder(root.right);
114+
res.add(root.val);
115+
}
116+
}
117+
118+
119+
120+
121+
```
122+
</TabItem>
123+
<TabItem value="C++" label="C++">
124+
<SolutionAuthor name="@parikhitkurmi"/>
125+
126+
```cpp
127+
//cpp
128+
129+
/**
130+
* Definition for a binary tree node.
131+
* struct TreeNode {
132+
* int val;
133+
* TreeNode *left;
134+
* TreeNode *right;
135+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
136+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
137+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
138+
* };
139+
*/
140+
class Solution {
141+
public:
142+
143+
vector<int> ans ;
144+
void postorder(TreeNode* root)
145+
{
146+
if(!root)
147+
return ;
148+
149+
postorder(root->left) ;
150+
postorder(root->right) ;
151+
152+
ans.push_back(root->val) ;
153+
154+
155+
}
156+
vector<int> postorderTraversal(TreeNode* root) {
157+
158+
postorder(root) ;
159+
return ans ;
160+
161+
}
162+
};
163+
164+
```
165+
166+
</TabItem>
167+
</Tabs>
168+
169+
170+
171+
172+
173+
## References
174+
175+
- **LeetCode Problem:** [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)
176+
- **Solution Link:** [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/submissions/)
177+
- **Authors GeeksforGeeks Profile:** [parikhit kurmi](https://www.geeksforgeeks.org/user/sololeveler673/)
178+
- **Authors Leetcode:** [parikhit kurmi](https://leetcode.com/u/parikhitkurmi14/)
179+

0 commit comments

Comments
 (0)