我不理解代码中的“继续”。 预期的输出:[1,2,3] 当我删除“ continue”时,输出:[1,2]; ... 这是leetcode的问题
https://leetcode.com/problems/binary-tree-preorder-traversal/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
helpByMorris(root, res);
return res;
}
private void helpByMorris(TreeNode root, List<Integer> res){
if(root == null) return;
TreeNode cur = root;
TreeNode mostRight = null;
while(cur != null){
mostRight = cur.left;
if(mostRight != null){
while(mostRight.right != null && mostRight.right != cur){
mostRight = mostRight.right;
}
if(mostRight.right == null){
mostRight.right = cur;
res.add(cur.val);
cur = cur.left;
continue; // here is my confusion
}else{
mostRight.right = null;
}
}else{
res.add(cur.val);
}
cur = cur.right;
}
}
}