Skip to content

Commit a2235ae

Browse files
Merge pull request neetcode-gh#3292 from shivammm21/main
Create 0144-binary-tree-preorder-traversal.java
2 parents 1ae934b + 8dc8ba6 commit a2235ae

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
18+
static void preOrder(TreeNode root, List<Integer> res){
19+
20+
21+
if(root == null){
22+
return;
23+
}
24+
res.add(root.val);
25+
preOrder(root.left,res);
26+
preOrder(root.right,res);
27+
28+
}
29+
30+
public List<Integer> preorderTraversal(TreeNode root) {
31+
32+
List<Integer> res = new ArrayList<>();
33+
34+
preOrder(root,res);
35+
36+
return res;
37+
38+
}
39+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy