/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public static Stack<TreeNode> stack;
public ArrayList<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> ret = new ArrayList<Integer>();
if(root==null)return ret;
stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode now = stack.pop();
ret.add(now.val);
if(now.right!=null)stack.push(now.right);
if(now.left!=null)stack.push(now.left);
}
return ret;
}
}
Binary Tree Preorder Traversal
最新推荐文章于 2022-11-29 02:00:43 发布