遍历线索化二叉树
遍历线索化二叉树 常规的线索化方式 采用递归地调用的方式,判定条件是当前指针的左子树是否为空 代码实现: public void midOrder ( ) { if ( this . left != null ) { this . left . midOrder ( ) ; } System . out . println ( this ) ; if ( this . right != null ) { this . right . midOrder ( ) ; } } 对比: 但是对二叉树进行线索化之后,不存在空的左右指针,但是单独设置每一个指针的类型,故而条件修改为指针的类型。 代码实现 我的代码 public void midLIst ( ) { if ( this . getLeftType ( ) != 1 ) { this . getLeft ( ) . midLIst ( ) ; } System . out . print ( this + " " ) ; if ( this . getRightType ( ) != 1 ) { this . getRight ( ) . midLIst ( ) ; } } 问题分析与总结: 出现空指针异常,因为到最后一个节点时,其右指针是没有改变,仍旧为null,并且没有对其值进行修改,故而会出现空指针。 代码修改 public