Java Solution with Explanation
\n\n# Approach and Explanation ( Recursive )\n Describe your approach to solving the problem. \n1. A preorder traversal is a traversal order in which the root n
\n\n# Approach and Explanation ( Recursive )\n<!-- Describe your approach to solving the problem. -->\n1. A preorder traversal is a traversal order in which the root node is visited first, followed by the left subtree, and then the right subtree. `The traversal order for a node is: root, left, right.`\n\n2. The code first defines a List called result which will store the preorder traversal of the tree. It then calls a helper function traversal which takes in a TreeNode called root and the result List.\n\n3. The traversal function first checks if the root is null. If it is, it returns without doing anything. If the root is not null, it adds the root\'s value to the result List. It then calls itself recursively on the root\'s left child and then on the root\'s right child.\n\n4. This recursive function will continue until it reaches a leaf node (a node with no children), at which point it will start returning and adding the values of the nodes it traversed on the way back up the recursive stack to the result List.\n\n5. At the end of the function, the result List will contain the preorder traversal of the tree.\n\n\n# Code\n```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n   public List<Integer> preorderTraversal(TreeNode root) {\n       // Create a list to store the traversal result\n\tList<Integer> result = new ArrayList<>();\n\t\n       // Call helper method to perform the traversal\n\ttraversal(root, result);\n\t\n       // Return the result\n\treturn result;\n   }\n   \n   // Helper method to perform the preorder traversal\n   public void traversal(TreeNode root, List<Integer> result) {\n       // Return if the current node is null\n\tif(root == null) return;\n\t\n       // Add the current node\'s value to the result list\n\tresult.add(root.val);\n\t\n       // Recursively traverse the left and right subtrees\n\ttraversal(root.left, result);\n\ttraversal(root.right, result);\n   }\n}\n```\n# Approach and Explanation ( Iterative )\n<!-- Describe your approach to solving the problem. -->\n\n1. This code is implementing the pre-order traversal of a binary tree. In a pre-order traversal, the root node is visited first, then the left subtree, and then the right subtree.\n\n2. The code is using a stack to store nodes as it traverses the tree. A stack is a Last-In-First-Out (LIFO) data structure, which means that the last element added to the stack will be the first one to be removed. This property is useful for implementing a tree traversal because it allows us to process the nodes in the correct order (i.e., root node before the child nodes).\n\n3. The algorithm works as follows:\n\n- Create an empty stack and an empty list to store the traversal.\n- Push the root node to the stack.\n- While the stack is not empty:\nPop the top element from the stack.\nAdd the popped element\'s value to the traversal list.\nPush the right child of the popped element to the stack.\nPush the left child of the popped element to the stack.\n- Return the traversal list.\n4. The reason why the right child is pushed to the stack before the left child is that we want to process the left subtree before the right subtree (since this is a pre-order traversal). Pushing the right child before the left child ensures that the left child will be processed first when it is popped from the stack.\n```\nclass Solution {\n    // This method returns a list of integers representing the preorder traversal of a binary tree\n    public List<Integer> preorderTraversal(TreeNode root) {\n     // Create an empty ArrayList to store the preorder traversal\n     ArrayList<Integer> result = new ArrayList<Integer>();\n      // If the root is null, return the empty list\n      if(root == null) \n      return result;\n      \n      // Create a stack to store the nodes as we traverse the tree\n      Stack<TreeNode> st = new Stack<TreeNode>();\n      // Push the root node onto the stack\n      st.push(root);\n      \n      // While the stack is not empty,\n      while(!st.isEmpty()) {\n        // Pop the top node from the stack\n        TreeNode current = st.pop(); \n        // Add the value of the node to the list\n        result.add(current.val);\n        \n        // If the current node has a right child, push it onto the stack\n        if(current.right != null) \n        st.push(current.right);\n        // If the current node has a left child, push it onto the stack\n        if(current.left != null)\n        st.push(current.left);\n      }\n      // Return the list of preorder traversal values\n      return result;\n    }\n}\n\n\n```\n
['Java']