hello-algo/codes/java/chapter_tree/binary_tree_dfs.java

69 lines
1.9 KiB
Java
Raw Normal View History

/**
* File: binary_tree_dfs.java
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
*/
2022-11-12 22:48:46 +08:00
package chapter_tree;
import include.*;
import java.util.*;
public class binary_tree_dfs {
// 初始化列表,用于存储遍历序列
static ArrayList<Integer> list = new ArrayList<>();
/* 前序遍历 */
static void preOrder(TreeNode root) {
if (root == null)
return;
// 访问优先级:根节点 -> 左子树 -> 右子树
2022-11-12 22:48:46 +08:00
list.add(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* 中序遍历 */
static void inOrder(TreeNode root) {
if (root == null)
return;
// 访问优先级:左子树 -> 根节点 -> 右子树
2022-11-12 22:48:46 +08:00
inOrder(root.left);
list.add(root.val);
inOrder(root.right);
}
/* 后序遍历 */
static void postOrder(TreeNode root) {
if (root == null)
return;
// 访问优先级:左子树 -> 右子树 -> 根节点
2022-11-12 22:48:46 +08:00
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));
2022-11-12 22:48:46 +08:00
System.out.println("\n初始化二叉树\n");
PrintUtil.printTree(root);
/* 前序遍历 */
list.clear();
preOrder(root);
System.out.println("\n前序遍历的节点打印序列 = " + list);
2022-11-12 22:48:46 +08:00
/* 中序遍历 */
list.clear();
inOrder(root);
System.out.println("\n中序遍历的节点打印序列 = " + list);
2022-11-12 22:48:46 +08:00
/* 后序遍历 */
list.clear();
postOrder(root);
System.out.println("\n后序遍历的节点打印序列 = " + list);
2022-11-12 22:48:46 +08:00
}
}