We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6346502 commit a18a609Copy full SHA for a18a609
java/0144-Binary-Tree-Preorder-Traversal.java
@@ -0,0 +1,16 @@
1
+class Solution {
2
+ public List<Integer> preorderTraversal(TreeNode root) {
3
+ List<Integer> res = new ArrayList<>();
4
+ Stack<TreeNode> stack = new Stack<>();
5
+ TreeNode current = root;
6
+ while(current!=null || !stack.isEmpty()){
7
+ while(current!=null){
8
+ res.add(current.val);
9
+ stack.add(current.right);
10
+ current = current.left;
11
+ }
12
+ current = stack.pop();
13
14
+ return res;
15
16
+}
0 commit comments