Skip to content

Commit 1bd4886

Browse files
committed
solved 144. Binary Tree Preorder Traversal
Signed-off-by: rajput-hemant <[email protected]>
1 parent 8cc44d4 commit 1bd4886

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

Diff for: TOPICWISE.md

+5
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
| **0104** | [Maximum Depth of Binary Tree][104] | Tree, BFS, DFS, Binary Tree | ![][easy] | |
9292
| **0110** | [Balanced Binary Tree][110] | Tree, DFS, Binary Tree | ![][easy] | |
9393
| **0111** | [Minimum Depth of Binary Tree][111] | Tree, DFS, BFS, Binary Tree | ![][easy] | |
94+
| **0144** | [Binary Tree Preorder Traversal][144] | Stack, Tree, DFS, Binary Tree | ![][easy] | |
9495
| **0199** | [Binary Tree Right Side View][199] | Tree, DFS, BFS, Binary Tree | ![][medium] | |
9596
| **0222** | [Count Complete Tree Nodes][222] | Binary Search, Tree, DFS, Binary Tree | ![][medium] | |
9697
| **0230** | [Kth Smallest Element in a BST ][230] | Tree, DFS, BST, Binary Tree | ![][medium] | |
@@ -135,6 +136,7 @@
135136
| **0104** | [Maximum Depth of Binary Tree][104] | Tree, BFS, DFS, Binary Tree | ![][easy] | |
136137
| **0110** | [Balanced Binary Tree][110] | Tree, DFS, Binary Tree | ![][easy] | |
137138
| **0111** | [Minimum Depth of Binary Tree][111] | Tree, DFS, BFS, Binary Tree | ![][easy] | |
139+
| **0144** | [Binary Tree Preorder Traversal][144] | Stack, Tree, DFS, Binary Tree | ![][easy] | |
138140
| **0199** | [Binary Tree Right Side View][199] | Tree, DFS, BFS, Binary Tree | ![][medium] | |
139141
| **0222** | [Count Complete Tree Nodes][222] | Binary Search, Tree, DFS, Binary Tree | ![][medium] | |
140142
| **0230** | [Kth Smallest Element in a BST ][230] | Tree, DFS, BST, Binary Tree | ![][medium] | |
@@ -170,6 +172,7 @@
170172
| **0104** | [Maximum Depth of Binary Tree][104] | Tree, BFS, DFS, Binary Tree | ![][easy] | |
171173
| **0110** | [Balanced Binary Tree][110] | Tree, DFS, Binary Tree | ![][easy] | |
172174
| **0111** | [Minimum Depth of Binary Tree][111] | Tree, DFS, BFS, Binary Tree | ![][easy] | |
175+
| **0144** | [Binary Tree Preorder Traversal][144] | Stack, Tree, DFS, Binary Tree | ![][easy] | |
173176
| **0199** | [Binary Tree Right Side View][199] | Tree, DFS, BFS, Binary Tree | ![][medium] | |
174177
| **0222** | [Count Complete Tree Nodes][222] | Binary Search, Tree, DFS, Binary Tree | ![][medium] | |
175178
| **0230** | [Kth Smallest Element in a BST ][230] | Tree, DFS, BST, Binary Tree | ![][medium] | |
@@ -201,6 +204,7 @@
201204
| # | Solution | Tags | Difficulty | Remark |
202205
| :------: | :----------------------------------------------: | :---------------------------: | :--------: | :----: |
203206
| **0094** | [Binary Tree Inorder Traversal][94] | Tree, Stack, DFS, Binary Tree | ![][easy] | |
207+
| **0144** | [Binary Tree Preorder Traversal][144] | Stack, Tree, DFS, Binary Tree | ![][easy] | |
204208
| **1047** | [Remove All Adjacent Duplicates In String][1047] | String, Stack | ![][easy] | |
205209

206210
<!--
@@ -592,6 +596,7 @@
592596
[104]: ./src/0101-0200/104%20-%20Maximum%20Depth%20of%20Binary%20Tree/
593597
[110]: ./src/0101-0200/110%20-%20Balanced%20Binary%20Tree/
594598
[111]: ./src/0101-0200/111%20-%20Minimum%20Depth%20of%20Binary%20Tree/
599+
[144]: ./src/0101-0200/144%20-%20Binary%20Tree%20Preorder%20Traversal/
595600
[172]: ./src/0101-0200/172%20-%20Factorial%20Trailing%20Zeroes/
596601
[199]: ./src/0101-0200/199%20-%20Binary%20Tree%20Right%20Side%20View/
597602
[222]: ./src/0201-0300/222%20-%20Count%20Complete%20Tree%20Nodes/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
import java.util.Stack;
4+
5+
import definitions.TreeNode;
6+
7+
public class BinaryTreePreorderTraversal {
8+
9+
List<Integer> list = new ArrayList<Integer>();
10+
11+
// Iterative Approach
12+
public List<Integer> preorderTraversal(TreeNode root) {
13+
if (root == null)
14+
return list;
15+
Stack<TreeNode> stack = new Stack<>();
16+
stack.push(root);
17+
while (!stack.isEmpty()) {
18+
TreeNode current = stack.pop();
19+
list.add(current.val);
20+
if (current.right != null)
21+
stack.push(current.right);
22+
if (current.left != null)
23+
stack.push(current.left);
24+
}
25+
return list;
26+
}
27+
28+
// Recursive Approach
29+
public List<Integer> preorderTraversalRecursive(TreeNode root) {
30+
if (root != null) {
31+
list.add(root.val);
32+
preorderTraversalRecursive(root.left);
33+
preorderTraversalRecursive(root.right);
34+
}
35+
return list;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 144. Binary Tree Preorder Traversal [![share]](https://leetcode.com/problems/binary-tree-preorder-traversal/submissions)
2+
3+
![][easy]
4+
5+
## Problem Statement:
6+
7+
Given the `root` of a binary tree, return the preorder traversal of its nodes' values.
8+
9+
![](https://assets.leetcode.com/uploads/2020/09/15/inorder_1.jpg)
10+
11+
### Example 1:
12+
13+
```
14+
Input: root = [1,null,2,3]
15+
Output: [1,2,3]
16+
```
17+
18+
### Example 2:
19+
20+
```
21+
Input: root = []
22+
Output: []
23+
```
24+
25+
### Example 3:
26+
27+
```
28+
Input: root = [1]
29+
Output: [1]
30+
```
31+
32+
### Constraints:
33+
34+
The number of nodes in the tree is in the range [0, 100].
35+
-100 <= Node.val <= 100
36+
37+
Follow up: Recursive solution is trivial, could you do it iteratively?
38+
39+
## Solution:
40+
41+
### [_Java_]()
42+
43+
```java
44+
List<Integer> list = new ArrayList<Integer>();
45+
46+
// Iterative Approach
47+
public List<Integer> preorderTraversal(TreeNode root) {
48+
if (root == null)
49+
return list;
50+
Stack<TreeNode> stack = new Stack<>();
51+
stack.push(root);
52+
while (!stack.isEmpty()) {
53+
TreeNode current = stack.pop();
54+
list.add(current.val);
55+
if (current.right != null)
56+
stack.push(current.right);
57+
if (current.left != null)
58+
stack.push(current.left);
59+
}
60+
return list;
61+
}
62+
63+
// Recursive Approach
64+
public List<Integer> preorderTraversalRecursive(TreeNode root) {
65+
if (root != null) {
66+
list.add(root.val);
67+
preorderTraversalRecursive(root.left);
68+
preorderTraversalRecursive(root.right);
69+
}
70+
return list;
71+
}
72+
```
73+
74+
### [_..._]()
75+
76+
```
77+
78+
```
79+
80+
<!----------------------------------{ link }--------------------------------->
81+
82+
[share]: https://img.icons8.com/external-anggara-blue-anggara-putra/20/000000/external-share-user-interface-basic-anggara-blue-anggara-putra-2.png
83+
[easy]: https://img.shields.io/badge/Difficulty-Easy-bright.svg
84+
[medium]: https://img.shields.io/badge/Difficulty-Medium-yellow.svg
85+
[hard]: https://img.shields.io/badge/Difficulty-Hard-red.svg

Diff for: src/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
| **0104** | [Maximum Depth of Binary Tree][104] | Tree, BFS, DFS, Binary Tree | ![][easy] | |
3131
| **0110** | [Balanced Binary Tree][110] | Tree, DFS, Binary Tree | ![][easy] | |
3232
| **0111** | [Minimum Depth of Binary Tree][111] | Tree, DFS, BFS, Binary Tree | ![][easy] | |
33+
| **0144** | [Binary Tree Preorder Traversal][144] | Stack, Tree, DFS, Binary Tree | ![][easy] | |
3334
| **0172** | [Factorial Trailing Zeroes][172] | Math | ![][medium] | |
3435
| **0199** | [Binary Tree Right Side View][199] | Tree, DFS, BFS, Binary Tree | ![][medium] | |
3536
| **0222** | [Count Complete Tree Nodes][222] | Binary Search, Tree, DFS, Binary Tree | ![][medium] | |
@@ -77,6 +78,7 @@
7778
[104]: ./0101-0200/104%20-%20Maximum%20Depth%20of%20Binary%20Tree/
7879
[110]: ./0101-0200/110%20-%20Balanced%20Binary%20Tree/
7980
[111]: ./0101-0200/111%20-%20Minimum%20Depth%20of%20Binary%20Tree/
81+
[144]: ./0101-0200/144%20-%20Binary%20Tree%20Preorder%20Traversal/
8082
[172]: ./0101-0200/172%20-%20Factorial%20Trailing%20Zeroes/
8183
[199]: ./0101-0200/199%20-%20Binary%20Tree%20Right%20Side%20View/
8284
[222]: ./0201-0300/222%20-%20Count%20Complete%20Tree%20Nodes/

0 commit comments

Comments
 (0)