From fefeae9a226769db3d7abc1864cc95f29b134d2d Mon Sep 17 00:00:00 2001
From: mahek0620 <136893675+mahek0620@users.noreply.github.com>
Date: Sat, 8 Jun 2024 14:36:21 +0530
Subject: [PATCH 1/5] Create 0144-binary-tree-preorder-traversal.md
---
.../0144-binary-tree-preorder-traversal.md | 167 ++++++++++++++++++
1 file changed, 167 insertions(+)
create mode 100644 dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md
diff --git a/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md b/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md
new file mode 100644
index 000000000..7f6f56cf6
--- /dev/null
+++ b/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md
@@ -0,0 +1,167 @@
+---
+id: binary tree preorder traversal
+title: Binary Tree Preorder Traversal
+sidebar_label: 0144 Binary Tree Preorder Traversal
+tags:
+ - tree
+ - LeetCode
+ - Python
+ - Java
+ - C++
+description: "This is a solution to the Binary Tree Preorder Traversal problem on LeetCode."
+---
+
+## Problem Description
+
+Given the root of a binary tree, return the preorder traversal of its nodes' values.
+
+### Examples
+
+**Example 1:**
+
+```
+
+
+Input: root = [1,null,2,3]
+Output: [1,2,3]
+```
+
+**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$
+
+
+#### Code in Different Languages
+
+
+
+
+ ```python
+ # Definition for a binary tree node.
+class TreeNode:
+ def __init__(self, val=0, left=None, right=None):
+ self.val = val
+ self.left = left
+ self.right = right
+
+class Solution:
+ def preorderTraversal(self, root):
+ if not root:
+ return []
+ result = []
+ stack = [root]
+ while stack:
+ node = stack.pop()
+ result.append(node.val)
+ if node.right:
+ stack.append(node.right)
+ if node.left:
+ stack.append(node.left)
+ return result
+
+ ```
+
+
+
+
+ ```java
+ import java.util.ArrayList;
+import java.util.List;
+import java.util.Stack;
+
+// Definition for a binary tree node.
+class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+ TreeNode(int x) { val = x; }
+}
+
+class Solution {
+ public List preorderTraversal(TreeNode root) {
+ List result = new ArrayList<>();
+ if (root == null)
+ return result;
+ Stack stack = new Stack<>();
+ stack.push(root);
+ while (!stack.isEmpty()) {
+ TreeNode node = stack.pop();
+ result.add(node.val);
+ if (node.right != null)
+ stack.push(node.right);
+ if (node.left != null)
+ stack.push(node.left);
+ }
+ return result;
+ }
+}
+
+
+
+ ```
+
+
+
+
+ ```cpp
+#include
+#include
+using namespace std;
+
+// Definition for a binary tree node provided by the precompiled header.
+struct TreeNode;
+
+class Solution {
+public:
+ vector preorderTraversal(TreeNode* root) {
+ if (!root)
+ return {};
+ vector result;
+ stack s;
+ s.push(root);
+ while (!s.empty()) {
+ TreeNode* node = s.top();
+ s.pop();
+ result.push_back(node->val);
+ if (node->right)
+ s.push(node->right);
+ if (node->left)
+ s.push(node->left);
+ }
+ return result;
+ }
+};
+
+
+
+ ```
+
+
+
+
+
+
+
+
+
+## References
+
+- **LeetCode Problem**: [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)
+
+- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/binary-tree-preorder-traversal/solution/)
+
+- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/)
From 439fbd2dc66564ffc1857454c1c944eb24496f3c Mon Sep 17 00:00:00 2001
From: mahek0620 <136893675+mahek0620@users.noreply.github.com>
Date: Sat, 8 Jun 2024 14:55:14 +0530
Subject: [PATCH 2/5] Create 0145-binary-tree-postorder-traversal.md
---
.../0145-binary-tree-postorder-traversal.md | 204 ++++++++++++++++++
1 file changed, 204 insertions(+)
create mode 100644 dsa-solutions/lc-solutions/0100-0199/0145-binary-tree-postorder-traversal.md
diff --git a/dsa-solutions/lc-solutions/0100-0199/0145-binary-tree-postorder-traversal.md b/dsa-solutions/lc-solutions/0100-0199/0145-binary-tree-postorder-traversal.md
new file mode 100644
index 000000000..c9032c284
--- /dev/null
+++ b/dsa-solutions/lc-solutions/0100-0199/0145-binary-tree-postorder-traversal.md
@@ -0,0 +1,204 @@
+---
+id: binary-tree-postorder-traversal
+title: Binary-Tree-Postorder-Traversal
+sidebar_label: 0145 Binary-Tree-Postorder-Traversal
+tags:
+ - DFS
+ - LeetCode
+ - Java
+ - Python
+ - C++
+description: "This is a solution to the Binary-Tree-Postorder-Traversal problem on LeetCode."
+---
+
+## Problem Description
+
+Given the root of a binary tree, return the postorder traversal of its nodes' values.
+
+### Examples
+
+**Example 1:**
+
+```
+
+
+Input: root = [1,null,2,3]
+Output: [3,2,1]
+
+```
+
+**Example 2:**
+
+
+```
+Input: root = []
+Output: []
+```
+
+**Example 3:**
+
+
+```
+Input: root = [1]
+Output: [1]
+```
+
+
+### Constraints
+
+- The number of the nodes in the tree is in the range [0, 100]
+- $-100 <= Node.val <= 100$
+
+
+---
+
+## Solution for Binary-Tree-Postorder-Traversal Problem
+
+### Intuition
+
+Imagine walking through the tree in a clockwise direction, starting from the left subtree, then moving to the right subtree, and finally reaching the root node. At each step, you collect the values of the nodes you visit, following the order: left, right, root.
+
+
+### Approach
+
+### Recursive Approach (DFS - Depth First Search):
+
+- Perform postorder traversal recursively by visiting the left subtree, then the right subtree, and finally the root node.
+- Base case: If the current node is null, return.
+- Recursively call the function on the left subtree.
+- Recursively call the function on the right subtree.
+- Append the value of the current node to the result list.
+- Return the result list.
+
+### Iterative Approach (Using Stack):
+
+- Start from the root node and initialize an empty stack.
+- While the stack is not empty or the current node is not null:
+- Traverse down the left subtree until you reach a leaf node, pushing each node onto the stack.
+- Once you reach a leaf node, pop the top node from the stack.
+- If the popped node has a right child and it is equal to the top of the stack, it means the right subtree has not been visited yet. In this case, pop the top node again and push the popped node back onto the stack, then move to its right child.
+- If the popped node does not have a right child or its right child has already been visited, append its value to the result list.
+- Return the result list.
+
+
+
+#### Code in Different Languages
+
+
+
+
+ ```python
+ # Definition for a binary tree node.
+class TreeNode:
+ def __init__(self, val=0, left=None, right=None):
+ self.val = val
+ self.left = left
+ self.right = right
+
+class Solution:
+ def postorderTraversal(self, root):
+ if not root:
+ return []
+ result = []
+ stack = [root]
+ while stack:
+ node = stack.pop()
+ result.append(node.val)
+ if node.left:
+ stack.append(node.left)
+ if node.right:
+ stack.append(node.right)
+ return result[::-1]
+
+
+
+ ```
+
+
+
+
+ ```java
+ import java.util.ArrayList;
+import java.util.List;
+import java.util.Stack;
+
+// Definition for a binary tree node.
+class TreeNode {
+ int val;
+ TreeNode left;
+ TreeNode right;
+ TreeNode(int x) { val = x; }
+}
+
+class Solution {
+ public List postorderTraversal(TreeNode root) {
+ List result = new ArrayList<>();
+ if (root == null)
+ return result;
+ Stack stack = new Stack<>();
+ stack.push(root);
+ while (!stack.isEmpty()) {
+ TreeNode node = stack.pop();
+ result.add(0, node.val); // Insert node.val at the beginning of the result list
+ if (node.left != null)
+ stack.push(node.left);
+ if (node.right != null)
+ stack.push(node.right);
+ }
+ return result;
+ }
+}
+
+
+ ```
+
+
+
+
+ ```cpp
+ #include
+#include
+using namespace std;
+
+// Definition for a binary tree node provided by the precompiled header.
+struct TreeNode;
+
+class Solution {
+public:
+ vector postorderTraversal(TreeNode* root) {
+ if (!root)
+ return {};
+ vector result;
+ stack s;
+ s.push(root);
+ while (!s.empty()) {
+ TreeNode* node = s.top();
+ s.pop();
+ result.push_back(node->val);
+ if (node->left)
+ s.push(node->left);
+ if (node->right)
+ s.push(node->right);
+ }
+ reverse(result.begin(), result.end());
+ return result;
+ }
+};
+
+
+ ```
+
+
+
+
+
+
+
+
+
+
+## References
+
+- **LeetCode Problem:** [Binary-Tree-Postorder-Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)
+- **Solution Link:** [Binary-Tree-Postorder-Traversal Solution on LeetCode](https://leetcode.com/problems/binary-tree-postorder-traversal/solutions/5273312/binary-tree-postorder-traversal-solution)
+- **Authors GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/)
From 5310ca571afc48303654979a09136c9915539abc Mon Sep 17 00:00:00 2001
From: mahek0620 <136893675+mahek0620@users.noreply.github.com>
Date: Sat, 8 Jun 2024 14:55:49 +0530
Subject: [PATCH 3/5] Update 0144-binary-tree-preorder-traversal.md
---
.../0100-0199/0144-binary-tree-preorder-traversal.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md b/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md
index 7f6f56cf6..9bbab90a9 100644
--- a/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md
+++ b/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md
@@ -4,6 +4,7 @@ title: Binary Tree Preorder Traversal
sidebar_label: 0144 Binary Tree Preorder Traversal
tags:
- tree
+ - DFS
- LeetCode
- Python
- Java
@@ -20,7 +21,6 @@ Given the root of a binary tree, return the preorder traversal of its nodes' val
**Example 1:**
```
-
Input: root = [1,null,2,3]
Output: [1,2,3]
From 2a3acd214997696b3ebbfd3a2441d126e39cb030 Mon Sep 17 00:00:00 2001
From: mahek0620 <136893675+mahek0620@users.noreply.github.com>
Date: Sun, 9 Jun 2024 11:19:22 +0530
Subject: [PATCH 4/5] Update 0144-binary-tree-preorder-traversal.md
---
.../0100-0199/0144-binary-tree-preorder-traversal.md | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md b/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md
index 9bbab90a9..f6654c6ce 100644
--- a/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md
+++ b/dsa-solutions/lc-solutions/0100-0199/0144-binary-tree-preorder-traversal.md
@@ -41,7 +41,7 @@ Output: [1]
### Constraints
-- $The number of nodes in the tree is in the range [0, 100].$
+- The number of nodes in the tree is in the range $[0, 100]$.
- $-100 <= Node.val <= 100$
@@ -154,9 +154,6 @@ public:
-
-
-
## References
From 78014ce5bc3f146488a0d522f318b540f5af58cc Mon Sep 17 00:00:00 2001
From: mahek0620 <136893675+mahek0620@users.noreply.github.com>
Date: Sun, 9 Jun 2024 11:20:09 +0530
Subject: [PATCH 5/5] Update 0145-binary-tree-postorder-traversal.md
---
.../0100-0199/0145-binary-tree-postorder-traversal.md | 2 --
1 file changed, 2 deletions(-)
diff --git a/dsa-solutions/lc-solutions/0100-0199/0145-binary-tree-postorder-traversal.md b/dsa-solutions/lc-solutions/0100-0199/0145-binary-tree-postorder-traversal.md
index c9032c284..ae9512dd1 100644
--- a/dsa-solutions/lc-solutions/0100-0199/0145-binary-tree-postorder-traversal.md
+++ b/dsa-solutions/lc-solutions/0100-0199/0145-binary-tree-postorder-traversal.md
@@ -192,9 +192,7 @@ public:
-
-
## References