Java || 2 Easy Approach with Explanation  || DSF and  Stack
\n1)\n//Recursive Solution //DFS\nclass Solution \n{\n    List<Integer> res= new ArrayList<>();//global ArrayList \n\n    public List<Integer> preorderTraversal
```\n1)\n//Recursive Solution //DFS\nclass Solution \n{\n    List<Integer> res= new ArrayList<>();//global ArrayList \n\n    public List<Integer> preorderTraversal(TreeNode root)\n    {\n         if(root==null)//base case for the null graph \n             return res;\n\n         preorder(root);//calling the preorder in order  \n\n         return res;//returning gthe resultant array list \n    }\n\n    public void preorder(TreeNode root)\n    {\n        if(root==null)//base case when we reach to the null node while recuring down hill, returning to the calling function and deleting the  activation record \n            return;//returning to the call function or the active acttib=vation record \n        \n        res.add(root.val);//adding first the value, as we first see the root we are adding it to the ArrayList (Root Left Right)\n        \n        preorder(root.left);//recursing down hill in search of left node \n        preorder(root.right);//after the left node is done fo the particular activation record , we are going downhill for the right node search \n        \n        return;//returning to the calling function or the active activation record and deleting this activation record as all the fuction are completed \n    }\n}//Please do Upvote, It helps a lot\n```\n```\n2)\n//Non-Recursive Solution \nclass Solution \n{\n    public List<Integer> preorderTraversal(TreeNode root) \n    {\n        List<Integer> list=new ArrayList<>();//for storing the element inorder \n        \n        if(root == null)//base case when the tree is empty\n            return list;\n        \n        Stack<TreeNode> stack= new Stack<>();\n        \n        stack.push(root);//pushing the root node \n        \n        while(!stack.isEmpty())//terminating condition \n        {\n            TreeNode temp=stack.pop();//popping the top element \n            \n            list.add(temp.val);//adding the value to the ArrayList //Root\n            \n            if(temp.right != null)//first pushing the right because to access the left first and then the right //Right\n                stack.push(temp.right);//pushing the right node \n            \n            if(temp.left != null)//pushing the left node if present after right because to access the left first //Left\n                stack.push(temp.left);\n        }//the main purpose of pushing this way is to achive Root Left Right pattern of Inorder Traversal \n        return list;//returning the List of integer that are stored in Inorder fashion \n    }\n}//Please do Upvote, It helps a lot\n```
['Stack', 'Recursion', 'Binary Tree', 'Iterator', 'Java']