题目:
题解:
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<Integer>();if (root == null) {return res;}TreeNode p1 = root, p2 = null;while (p1 != null) {p2 = p1.left;if (p2 != null) {while (p2.right != null && p2.right != p1) {p2 = p2.right;}if (p2.right == null) {res.add(p1.val);p2.right = p1;p1 = p1.left;continue;} else {p2.right = null;}} else {res.add(p1.val);}p1 = p1.right;}return res;}
}