/** * Definition of ParentTreeNode: * * class ParentTreeNode { * public ParentTreeNode parent, left, right; * } */publicstaticvoidprintPreorder(TreeNode root) {TreeNode cur = root;while(cur !=null){System.out.println(cur.val);if(cur.left!=null){ cur =cur.left; } elseif(cur.right!=null){ cur =cur.right; } else {while(cur.parent!=null&& (cur.parent.right==null|| cur ==cur.parent.right)){ cur =cur.parent; }if(cur.parent==null) break; cur =cur.parent.right; } }}