赞
踩
// 定义二叉树的节点类
class TreeNode {
int value;
TreeNode left;
TreeNode right;
public TreeNode(int value) {
this.value = value;
left = null;
right = null;
}
}
// 二叉树的定义和创建方法 public class BinaryTree { private TreeNode root; public BinaryTree() { root = null; } public void createTree(int[] values) { root = createTreeHelper(values, 0); } private TreeNode createTreeHelper(int[] values, int index) { if (index >= values.length) { return null; } TreeNode newNode = new TreeNode(values[index]); newNode.left = createTreeHelper(values, 2 * index + 1); newNode.right = createTreeHelper(values, 2 * index + 2); return newNode; } }
public static void preorderTraversalHelper(TreeNode node) {
if (node != null) {
System.out.print(node.value + " ");
preorderTraversalHelper(node.left);
preorderTraversalHelper(node.right);
}
}
public static void preorderTraversalHelper(TreeNode node) {
if (node != null) {
preorderTraversalHelper(node.left);
System.out.print(node.value + " ");
preorderTraversalHelper(node.right);
}
}
public static void preorderTraversalHelper(TreeNode node) {
if (node != null) {
preorderTraversalHelper(node.left);
preorderTraversalHelper(node.right);
System.out.print(node.value + " ");
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。