Skip to content

Commit

Permalink
0144 problem added
Browse files Browse the repository at this point in the history
  • Loading branch information
shivammm21 committed Feb 10, 2024
1 parent 08c263e commit 8dc8ba6
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 8dc8ba6

Please sign in to comment.