文章

7

粉丝

204

获赞

4

访问

64.4k

头像
判断二叉树是否对称
P1552
发布于2020年1月18日 21:49
阅读数 7.7k

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

class Main {

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		String str = cin.next();
		Queue<Character> q = new LinkedList<Character>();
		for (int i = 0; i < str.length(); i++)
			q.offer(str.charAt(i));
		Node node = StringToTree(q);
		System.out.println(istt(node) == true ? "YES" : "NO");
	}

	static boolean istt(Node node) {
		if (node == null)
			return true;
		return istt(node.left, node.right);
	}

	static boolean istt(Node left, Node right) {
		if (left == null && right == null)
			return true;
		if (left == null || right == null)
			return false;
		return istt(left.left, right.right) && istt(left.right, right.left);
	}

	public static Node StringToTree(Queue<Character> q) {
		char s = q.poll();
		if (s == '#')
			return null;

		Node head = new Node(s);
		Queue<Node> queue1 = ne...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发