输出根结点到叶子结点的路径
[!Tip]
本节源代码见Github链接🔗
问题描述
对于一棵给定的二叉树,输出所有从根结点到叶子结点的路径
核心思路
先序遍历二叉树,并用数组保存遍历过的元素,当遍历到叶子结点时,输出数组中的元素
实现代码
【👉🏻>>点击展开查看代码】
/**
* 输出所有从根结点到叶子结点的路径
*
* @className: PrintPaths
* @author: Max Solider
* @date: 2023-06-11 22:41
*/public class PrintPaths {
public static void printPaths(BinaryTreeNode root) {
int[] path = new int[256];
printPaths(root, path, 0);
}
private static void printPaths(BinaryTreeNode root, int[] path, int pathLen) {
if (root == null) {
return;
}
path[pathLen] = root.getData();
pathLen++;
if (root.getLeft() == null && root.getRight() == null) {
printArray(path, pathLen);
} else {
printPaths(root.getLeft(), path, pathLen);
printPaths(root.getRight(), path, pathLen);
}
}
private static void printArray(int[] path, int pathLen) {
for (int i = 0; i < pathLen; i++) {
System.out.print(path[i] + " ");
}
System.out.println("");
}
public static void main(String[] args) {
BinaryTreeNode node4 = new BinaryTreeNode(4, null, null);
BinaryTreeNode node5 = new BinaryTreeNode(5, null, null);
BinaryTreeNode node6 = new BinaryTreeNode(6, null, null);
BinaryTreeNode node7 = new BinaryTreeNode(7, null, null);
BinaryTreeNode node2 = new BinaryTreeNode(2, node4, node5);
BinaryTreeNode node3 = new BinaryTreeNode(3, node6, node7);
BinaryTreeNode node1 = new BinaryTreeNode(1, node2, node3);
printPaths(node1);
}
}
时间复杂度
时间复杂度为O(n)。
空间复杂度
空间复杂度为O(n)。