
用java编写一个二叉树的三种遍历的程序。急用,高手帮个忙哪
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
前序遍历:
public static void preorder(TreeNode root)
{
if(root==null) return;
System.out.print(root.val+" ");
preorder(root.left);
preorder(root.right);
}
中序遍历:
public static void inorder(TreeNode root)
{
if(root==null) return;
inorder(root.left);
System.out.print(root.val+" ");
inorder(root.right);
}
后序遍历:
public static void postorder(TreeNode root)
{
if(root==null) return;
postorder(root.left);
postorder(root.right);
System.out.print(root.val+" ");
}
二叉树类:
class TreeNode
{
int val;
TreeNode left=null;
TreeNode right=null;
public TreeNode(int x){val=x;}
}
public static void preorder(TreeNode root)
{
if(root==null) return;
System.out.print(root.val+" ");
preorder(root.left);
preorder(root.right);
}
中序遍历:
public static void inorder(TreeNode root)
{
if(root==null) return;
inorder(root.left);
System.out.print(root.val+" ");
inorder(root.right);
}
后序遍历:
public static void postorder(TreeNode root)
{
if(root==null) return;
postorder(root.left);
postorder(root.right);
System.out.print(root.val+" ");
}
二叉树类:
class TreeNode
{
int val;
TreeNode left=null;
TreeNode right=null;
public TreeNode(int x){val=x;}
}
追问
谢谢谢谢!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询