题目链接

    1. // 中序mirror
    2. public List<Integer> inorderTraversal(TreeNode root) {
    3. mirrisPre(root);
    4. return ansmorris;
    5. }
    6. List<Integer> ansmorris = new ArrayList<>();
    7. public void mirrisPre(TreeNode cur) {
    8. if(cur == null) return;
    9. TreeNode mostRight = null;
    10. while(cur != null) {
    11. mostRight = cur.left;
    12. if(mostRight != null) {
    13. while(mostRight.right != null && mostRight.right != cur) { // 如果右边不为空,并且右边不指向后继节点的话就找到其前继节点
    14. mostRight = mostRight.right;
    15. }
    16. if(mostRight.right == null) { // 建立线索指针
    17. mostRight.right = cur;
    18. // 打印
    19. // ansmorris.add(cur.val);
    20. cur = cur.left;
    21. continue;
    22. } else { // 删除线索指针
    23. mostRight.right = null;
    24. }
    25. } else {
    26. }
    27. // 打印节点
    28. ansmorris.add(cur.val);
    29. cur = cur.right; // 向右走
    30. }
    31. }

    前序遍历

        // 前序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; // 向右走
             }
         }