PAT甲级1043 Is It a Binary Search Tree(JAVA版)

31 篇文章 0 订阅

本题考查二叉搜索树的构建,与树的先序遍历、后序遍历。

思路:按照二叉搜索树规则构建二叉树,对该树进行先序与镜像先序遍历,若两个顺序的其中一个与题目给的相符,则输出YES,并输出其后序遍历结果。

AC代码:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scaner = new Scanner(System.in);
		Tree tree = new Tree();
		int round = scaner.nextInt();
		String ori = "";
		for(int i = 0 ; i < round ; i++) {
			int temp = scaner.nextInt();
			ori += temp + " ";
			tree.insert(temp);
		}
		scaner.close();
		ori = ori.trim();
		String pre = tree.preOrder(tree.root,"").trim();
		String mPre = tree.mPreOrder(tree.root,"").trim();
		if(ori.equals(pre)) {
			System.out.println("YES");
			String postResult = tree.postOrder(tree.root,"").trim();
			System.out.println(postResult);
		}
		else if(ori.equals(mPre)) {
			System.out.println("YES");
			String mPostResult = tree.mPostOrder(tree.root,"").trim();
			System.out.println(mPostResult);
		}
		else {
			System.out.println("NO");
		}
	}
}

class Node{
	private int key;
	private Node leftChild;
	private Node rightChild;
	public Node(int key) {
		this.key = key;
		leftChild = null;
		rightChild = null;
	}
	public int getKey() {
		return key;
	}
	public void setLeftChild(Node leftChild) {
		this.leftChild = leftChild;
	}
	public void setRightChild(Node rightChild) {
		this.rightChild = rightChild;
	}
	public Node getLeftChild() {
		return leftChild;
	}
	public Node getRightChild() {
		return rightChild;
	}
}

class Tree{
	Node root;
	public Tree() {
		root = null;
	}
	
	public void insert(int key) {
		if(root == null) {
			root = new Node(key);
		}
		else {
			Node temp = root;
			while(true) {
				if(key < temp.getKey()) {
					if(temp.getLeftChild() == null) {
						temp.setLeftChild(new Node(key));
						break;
					}
					else
						temp = temp.getLeftChild();
				}
				else {
					if(temp.getRightChild() == null) {
						temp.setRightChild(new Node(key));
						break;
					}
					else
						temp = temp.getRightChild();
				}
			}
		}
	}
	
	public String preOrder(Node root , String result) {
		result += root.getKey() + " ";
		Node leftChild = root.getLeftChild();
		if(leftChild != null) {
			result = preOrder(leftChild , result);
		}
		Node rightChild = root.getRightChild();
		if(rightChild != null) {
			result =  preOrder(rightChild , result);
		}
		return result;
	}
	
	public String postOrder(Node root , String result) {
		Node leftChild = root.getLeftChild();
		if(leftChild != null) {
			result = postOrder(leftChild , result);
		}
		Node rightChild = root.getRightChild();
		if(rightChild != null) {
			result =  postOrder(rightChild , result);
		}
		result += root.getKey() + " ";
		return result;
	}
	
	public String mPreOrder(Node root , String result) {
		result += root.getKey() + " ";
		Node rightChild = root.getRightChild();
		if(rightChild != null) {
			result =  mPreOrder(rightChild , result);
		}
		Node leftChild = root.getLeftChild();
		if(leftChild != null) {
			result = mPreOrder(leftChild , result);
		}
		return result;
	}
	
	public String mPostOrder(Node root , String result) {
		Node rightChild = root.getRightChild();
		if(rightChild != null) {
			result =  mPostOrder(rightChild , result);
		}
		Node leftChild = root.getLeftChild();
		if(leftChild != null) {
			result = mPostOrder(leftChild , result);
		}
		result += root.getKey() + " ";
		return result;
	}
}

参考网址:

  1. https://blog.csdn.net/coder__666/article/details/80349039
  2. https://blog.csdn.net/a845717607/article/details/86674999(C++版)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值