二叉樹三種遞迴遍歷方式
阿新 • • 發佈:2019-02-02
- /**
- *
- * 遞迴先序遍歷
- */
- publicstaticvoid preOrderRec(Node root){
- if(root!=null){
- System.out.println(root.value);
- preOrderRec(root.left);
- preOrderRec(root.right);
- }
- }
- /**
- *
- * 遞迴中序遍歷
- */
-
public
- if(root!=null){
- preOrderRec(root.left);
- System.out.println(root.value);
- preOrderRec(root.right);
- }
- }
- /**
- *
- * 遞迴後序遍歷
- */
- publicstaticvoid postOrderRec(Node root){
-
if
- preOrderRec(root.left);
- preOrderRec(root.right);
- System.out.println(root.value);
- }
- }