java实现DFS深度遍历二叉树

用栈实现DFS

    public static <T extends Comparable<T>> void dfs(BTree<T> tree) {
		dfs(tree.root);
		System.out.println();
	}

	private static <T extends Comparable<T>> void dfs(BTree<T>.BNode node) {
		if (node == null)
			return;
		stack.push(node);
		System.out.print(node.value + "  ");
		dfs(node.left);
		dfs(node.right);
		stack.pop();
	}

点赞