二叉搜索树比较

   前言:

       二叉搜索树是二叉排序树,左子树比根小,右子树比根大,所以它建树的方式和普通建树的方式稍有不同,每次都要和根节点往下比较而确定位置,然后采用遍历二叉树节点的方式确定两棵树是否完全相等。

    言归正传,在牛客网页编译器里运行结果有出入,但是在本地调试是没有问题的,而且检验,也没找到有什么逻辑错,这个问题存疑~~~

     在8月23号再拿起这道题目后,发现问题的所在:在对持续输入的数据处理时,处理完一组数要及时清理,不然有残留数据没有清空会影响后面的计算,这里bTree对象没有及时清空导致出错,把它的初始化放到while循环内就正确了。

题目描述

判断两序列是否为同一二叉搜索树序列 
输入描述:
开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束。
接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树。
接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树。
输出描述:
如果序列相同则输出YES,否则输出NO

输入例子:
2
567432
543267
576342
0

输出例子:
YES
NO

代码如下:
import java.util.Scanner;

/**
 * 二叉搜索树比较相同
 * 
 * @author ygh
 *
 */
public class BinaryTree2 {
    private Node root;
    private static int count = 0;

    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        while (true) {
            int n = in.nextInt();
            if (n == 0)
                break;
            //初始化两颗树  对象
            BinaryTree2 bTree = new BinaryTree2();
            BinaryTree2 bTree2 = new BinaryTree2();
            char[] tree = in.next().toCharArray();
            for (int a = 0; a < tree.length; a++) {
                bTree.createTree(bTree.root, tree[a]);
            }
            for (int i = 0; i < n; i++) {
                char[] str = in.next().toCharArray();
                if (str.length != tree.length) {
                    System.out.println("NO");
                    break;
                }
                for (int a = 0; a < str.length; a++) {
                    bTree2.createTree(bTree2.root, str[a]);
                }
                orderTree(bTree.root, bTree2.root);
                if (count == str.length)
                    System.out.println("YES");
                else
                    System.out.println("NO");
                count = 0;
                 bTree2=new BinaryTree2();
            }
        }
        in.close();
    }

    private class Node {
        private Node left;
        private Node right;
        private char data;

        public Node(char data) {
            this.left = null;
            this.right = null;
            this.data = data;
        }
    }
    
    /**
     * 创建二叉搜索树
     * @param node
     * @param data
     */
    public void createTree(Node node, char data) {
        if (root == null)
            root = new Node(data);
        else {
            if (data < node.data) {
                if (node.left == null) {
                    node.left = new Node(data);
                } else {
                    createTree(node.left, data);
                }
            } else {
                if (node.right == null) {
                    node.right = new Node(data);
                } else {
                    createTree(node.right, data);
                }
            }
        }

    }
   
    /**
     * 比较两颗树的相同程度
     * @param node1
     * @param node2
     */
    public static void orderTree(Node node1, Node node2) {
        if (node1 != null && node2 != null) {
            if (node1.data == node2.data) {
                count++;
            }
            orderTree(node1.left, node2.left);
            orderTree(node1.right, node2.right);

        }
    }

}

 



 




posted @ 2016-08-05 20:03  华不摇曳  阅读(426)  评论(0编辑  收藏  举报