Untitled
class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list = new ArrayList(); if(root == null) return list; Stack<TreeNode> stack = new Stack(); stack.add(root); while(!stack.isEmpty()) { root = stack.pop(); list.add(root.val); if(root.right != null) stack.add(root.right); if(root.left != null) stack.add(root.left); } return list; } }
Leave a Comment