递归
//递归先序遍历
public void preOrderRecursive(TreeNode root) {
if (root == null) return;// 递归的终止条件
System.out.print(root.value + " ");// 访问左右子节点*之前*,访问当前节点数据并打印
preOrderRecursive(root.left);// 先序遍历左子节点
preOrderRecursive(root.right);// 先序遍历右子节点
}
// 递归中序遍历二叉树
public void inOrderRecursive(TreeNode root) {
if (root == null) return;// 递归的终止条件
inOrderRecursive(root.left);// 中序遍历左子节点
System.out.print(root.value + " ");// 访问左右子节点*之间*,访问当前节点数据并打印
inOrderRecursive(root.right);// 中序遍历右子节点
}
// 递归后序遍历二叉树
public void postOrderRecursive(TreeNode root) {
if (root == null) return;// 递归的终止条件
postOrderRecursive(root.left);// 后序遍历左子节点
postOrderRecursive(root.right);// 后序遍历右子节点
System.out.print(root.value + " ");// 访问左右子节点*之后*,访问当前节点数据并打印
}
Last updated