// 中序mirror
public List<Integer> inorderTraversal(TreeNode root) {
mirrisPre(root);
return ansmorris;
}
List<Integer> ansmorris = new ArrayList<>();
public void mirrisPre(TreeNode cur) {
if(cur == null) return;
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;
// 打印
// ansmorris.add(cur.val);
cur = cur.left;
continue;
} else { // 删除线索指针
mostRight.right = null;
}
} else {
}
// 打印节点
ansmorris.add(cur.val);
cur = cur.right; // 向右走
}
}
前序遍历
// 前序mirror
public List<Integer> inorderTraversal(TreeNode root) {
mirrisPre(root);
return ansmorris;
}
List<Integer> ansmorris = new ArrayList<>();
public void mirrisPre(TreeNode cur) {
if(cur == null) return;
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;
// 打印
ansmorris.add(cur.val);
cur = cur.left;
continue;
} else { // 删除线索指针
mostRight.right = null;
}
} else {
ansmorris.add(cur.val);
}
cur = cur.right; // 向右走
}
}