You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.8 KiB
69 lines
1.8 KiB
/**
|
|
* File: binary_tree_dfs.java
|
|
* Created Time: 2022-11-25
|
|
* Author: Krahets (krahets@163.com)
|
|
*/
|
|
|
|
package chapter_tree;
|
|
|
|
import utils.*;
|
|
import java.util.*;
|
|
|
|
public class binary_tree_dfs {
|
|
// 初始化列表,用于存储遍历序列
|
|
static ArrayList<Integer> list = new ArrayList<>();
|
|
|
|
/* 前序遍历 */
|
|
static void preOrder(TreeNode root) {
|
|
if (root == null)
|
|
return;
|
|
// 访问优先级:根节点 -> 左子树 -> 右子树
|
|
list.add(root.val);
|
|
preOrder(root.left);
|
|
preOrder(root.right);
|
|
}
|
|
|
|
/* 中序遍历 */
|
|
static void inOrder(TreeNode root) {
|
|
if (root == null)
|
|
return;
|
|
// 访问优先级:左子树 -> 根节点 -> 右子树
|
|
inOrder(root.left);
|
|
list.add(root.val);
|
|
inOrder(root.right);
|
|
}
|
|
|
|
/* 后序遍历 */
|
|
static void postOrder(TreeNode root) {
|
|
if (root == null)
|
|
return;
|
|
// 访问优先级:左子树 -> 右子树 -> 根节点
|
|
postOrder(root.left);
|
|
postOrder(root.right);
|
|
list.add(root.val);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
/* 初始化二叉树 */
|
|
// 这里借助了一个从数组直接生成二叉树的函数
|
|
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
|
|
System.out.println("\n初始化二叉树\n");
|
|
PrintUtil.printTree(root);
|
|
|
|
/* 前序遍历 */
|
|
list.clear();
|
|
preOrder(root);
|
|
System.out.println("\n前序遍历的节点打印序列 = " + list);
|
|
|
|
/* 中序遍历 */
|
|
list.clear();
|
|
inOrder(root);
|
|
System.out.println("\n中序遍历的节点打印序列 = " + list);
|
|
|
|
/* 后序遍历 */
|
|
list.clear();
|
|
postOrder(root);
|
|
System.out.println("\n后序遍历的节点打印序列 = " + list);
|
|
}
|
|
}
|