Skip to content

Commit

Permalink
Merge pull request neetcode-gh#3292 from shivammm21/main
Browse files Browse the repository at this point in the history
Create 0144-binary-tree-preorder-traversal.java
  • Loading branch information
sujal-goswami committed Feb 15, 2024
2 parents 1ae934b + 8dc8ba6 commit a2235ae
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions java/0144-binary-tree-preorder-traversal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {

static void preOrder(TreeNode root, List<Integer> res){


if(root == null){
return;
}
res.add(root.val);
preOrder(root.left,res);
preOrder(root.right,res);

}

public List<Integer> preorderTraversal(TreeNode root) {

List<Integer> res = new ArrayList<>();

preOrder(root,res);

return res;

}
}

0 comments on commit a2235ae

Please sign in to comment.