Untitled
unknown
plain_text
10 months ago
552 B
3
Indexable
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;
}
}Editor is loading...
Leave a Comment