Morris in order binary tree traversal algorithm

Morris in order binary tree traversal algorithm

1. Definition of the tree node

class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

2.Morris algorithm steps :( core is its connection to the preamble of the algorithm)

①若当前节点左子树为空(左子树为空那么其没有前序节点),那么打印当前节点进入右子树
②若当前节点左子树不为空(说明它有前序节点),看其前序节点的右指针,其实只有两种可能,
1.要么指向它本身:如果指向当前节点(这个时候相当于左子树已经遍历完了,又回到这里的)
,打印当前节点,进入右节点(如果你想保持树的原始结构,可以把前序节点的右指针改回初始状态,即null)
2.要么指向空:(当然刚开始初始状态当然是是空),如果是空,那么将其指向当前节点,进入左子树

最后就是上述过程的出口:当前节点为空是则结束该算法

3. code implementation

The algorithm does not help other auxiliary space, the reign of the algorithm is good (the algorithm drawing their own proposals to go again, a deeper impression)
above algorithms mentioned in the preamble of obtaining node, Now I give the code

A predecessor code for obtaining

  public  TreeNode getPredecessor(TreeNode current)//获取中序遍历的前序节点
    {
        if(current==null||current.left==null)
            return null;
        TreeNode pre = current.left;
        while(pre.right!=null&&pre.right!=current)//不可以只是null,因为加了加了前序连接,否则出现死循环
            pre=pre.right;
        //System.out.println(pre.val);
        return  pre;
    }

Morris algorithm code implementation

//该算法严格按照上面的步骤实现
public void MorrisTravel(TreeNode root)//莫里斯中序遍历
    {
        TreeNode pre;
        TreeNode current = root;
        while(current!=null)
        {
            if(current.left==null)
            {
                System.out.println(current.val);
                current=current.right;
            }
            else
            {
                pre = getPredecessor(current);
                if(pre.right==null)
                {
                 	pre.right=current;
                	current = current.left;
                }
                else if(pre.right==current)
                {
                    pre.right=null;//这里我是为了保持树的原始结构
                    System.out.println(current.val);
                    current = current.right;
                }
            }
        }
    }

Keep up to next!

Published 22 original articles · won praise 0 · Views 590

Guess you like

Origin blog.csdn.net/qq_44932835/article/details/105271498