前序,中序,后序和层序遍历(Java)的n中写法

 

前序,中序和后序除递归方法外,推荐前序非递归3,中序非递归2,后序非递归(这个以后要回来看,明天怕是就会忘掉),层序以后可以关注一下有没有递归的方法。

class TreeNode{
	int val;
	TreeNode left,right;
	public TreeNode(int val) {
		super();
		this.val = val;
	}
	public TreeNode(int val, TreeNode left, TreeNode right) {
		super();
		this.val = val;
		this.left = left;
		this.right = right;
	}
	//前序递归
	public static String preOrder(TreeNode root) {
		StringBuilder builder=new StringBuilder();
		preOrderHelper(root, builder);
		return builder.toString();
	}
	public static void preOrderHelper(TreeNode n,StringBuilder builder) {
		if(n==null)return;
		//如果遇到需要判断叶子结点的需要以下代码
//		TreeNode left=n.left;
//		TreeNode right=n.right;
//		if(left==null&&right==null)return;
		builder.append(n.val+" ");
		preOrderHelper(n.left, builder);
		preOrderHelper(n.right,builder);
		
	}
	//前序非递归1
	public static String preOrderNonRecursive1(TreeNode root) {
		StringBuilder builder=new StringBuilder();
		Stack<TreeNode> stack=new Stack<>();
		stack.push(root);
		while(stack.size()!=0) {
			TreeNode n=stack.pop();
			if(n==null)continue;
			builder.append(n.val+" ");
			stack.push(n.right);
			stack.push(n.left);
		}
		return builder.toString();
	}
	
	//前序非递归2
	public static String preOrderNonRecursive2(TreeNode root) {
		StringBuilder builder=new StringBuilder();
		Stack<TreeNode> stack=new Stack<>();
		while(true) {
			while(root!=null) {
				builder.append(root.val+" ");
				stack.push(root);
				root=root.left;
			}
			if(stack.isEmpty())break;
			root=stack.pop();
			root=root.right;
		}
		return builder.toString();
	}
	//前序非递归写法3
	public static String preOrderNonRecursive3(TreeNode root) {
		StringBuilder builder=new StringBuilder();
		Stack<TreeNode> stack=new Stack<>();
		while(root!=null||!stack.isEmpty()) {
			if(root!=null) {
				stack.push(root);
				builder.append(root.val);
				root=root.left;
			}
			else {
				root=stack.pop();
				root=root.right;
			}
		}
		return builder.toString();
	}
	
	//中序遍历递归
	public static String inOrderRecursive(TreeNode root) {
		StringBuilder builder=new StringBuilder();
		inOrderHelper(root, builder);
		return builder.toString();
	}
	public static void inOrderHelper(TreeNode n,StringBuilder builder) {
		if(n==null)return;
		inOrderHelper(n.left, builder);
		builder.append(n.val+" ");
		inOrderHelper(n.right, builder);
	}
	//中序遍历非递归写法1
	public static String inOrderNonRecursive(TreeNode root) {
		Stack<TreeNode> stack=new Stack<>();
		StringBuilder builder=new StringBuilder();
		while(true) {
			while(root!=null) {
				stack.push(root);
				root=root.left;
			}
			if(stack.isEmpty())break;
			root=stack.pop();
			builder.append(root.val+" ");
			root=root.right;
		}
		return builder.toString();
	}
	//中序非递归写法2
	public static String inOrderNonRecursive2(TreeNode root) {
		StringBuilder builder=new StringBuilder();
		Stack<TreeNode> stack=new Stack<>();
		while(root!=null||!stack.isEmpty()) {
			if(root!=null) {
				stack.push(root);
				root=root.left;
			}else {
				root=stack.pop();
				builder.append(root.val);
				root=root.right;
			}
		}
		return builder.toString();
	}
	//后序递归
	public static String postOrderRecursive(TreeNode root) {
		StringBuilder builder=new StringBuilder();
		postOrderHelper(root, builder);
		return builder.toString();
	}
	public static void postOrderHelper(TreeNode n,StringBuilder builder) {
		if(n==null)return;
		postOrderHelper(n.left, builder);
		postOrderHelper(n.right, builder);
		builder.append(n.val);
	}
	//后序非递归
	public static String postOrderNonRecursive(TreeNode root) {
		Stack<TreeNode> stack=new Stack<>();
		StringBuilder builder=new StringBuilder();
		TreeNode pre=null,curr;
		stack.push(root);
		while(!stack.isEmpty()) {
			curr=stack.peek();
			if((curr.left==null&&curr.right==null)||(pre!=null&&(pre==curr.left||pre==curr.right))) {
				builder.append(curr.val);
				stack.pop();
				pre=curr;
			}else {
				if(curr.right!=null)
					stack.push(curr.right);
				if(curr.left!=null)
					stack.push(curr.left);
			}
		}
		return builder.toString();
	}
	//层序遍历好像还没有递归的方法,因为他一层一层,有很多结点。
	public static String BFS(TreeNode root) {
		StringBuilder builder=new StringBuilder();
		LinkedList<TreeNode> queue=new LinkedList<>();
		queue.offer(root);
		while(!queue.isEmpty()) {
			TreeNode n=queue.poll();
			if(n==null)continue;
			builder.append(n.val);
			queue.offer(n.left);
			queue.offer(n.right);
		}
		return builder.toString();
	}
	
	@Override
	public String toString() {
		return "TreeNode [val=" + val +"]";
	}
}
  • 利用了上面的中序遍历非递归方法3,完成了题目783。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值