判断给定的二叉树是否为二分查找树

来源:互联网 发布:ios 蜂窝移动网络搜索 编辑:程序博客网 时间:2024/05/22 08:04

笔试题:
判断给定的二叉树是否为二分查找树。假设树的每个节点都是以整数为键值,且不同节点值不相同。
返回结果:是二分查找树返回true,否则返回false。
输入:
5
5:2|1
2:-1|-1
1:-1|-1

5
5:2|8
2:-1|-1
8:-1|-1

package com.hedx.test.Demo;import java.util.Scanner;import java.util.Stack;public class BST {    // 二叉树节点定义    public static class TreeNode {        int val;        TreeNode left;        TreeNode right;        public TreeNode(int data) {            this.val = data;        }    }    public static void main(String[] args) {        // TODO Auto-generated method stub        TreeNode root = null;        int count = 0;         int countNode = 0;        Scanner reader = new Scanner(System.in);        int a = reader.nextInt();        reader.nextLine();        root = new TreeNode(a);        countNode++;        String string = reader.nextLine();        /**         * 5          * 5:2|1          * 2:-1|-1          * 1:-1|-1         * 控制带的输入二叉树样式;         * 不同的输入方式在这里处理不同         */        while (string != null && !"".equals(string)) {            String[] strings = string.split(":");            int insertnode = Integer.parseInt(strings[0]);            //查找节点,加入该节点的子节点            TreeNode temp = searchNode(insertnode, root);            if (temp == null) {                break;            }            String[] strings2 = strings[1].split("\\|");            if (!"-1".equals(strings2[0])) {                temp.left = new TreeNode(Integer.parseInt(strings2[0]));                countNode++;            } else {                count++;                temp.left = null;            }            if (!"-1".equals(strings2[1])) {                temp.right = new TreeNode(Integer.parseInt(strings2[1]));                countNode++;            } else {                count++;                temp.right = null;            }            //判断该二叉树是否输入完整            if (countNode == count - 1) {                break;            }            string = reader.nextLine();        }        System.out.println(isValidBST(root));    }    //查找已建立二叉树中某确定值的节点    public static TreeNode searchNode(Integer target, TreeNode startNode) {        int compareResult = target.compareTo(startNode.val);        if (compareResult == 0)            return startNode;        else if (target.compareTo(startNode.right.val) == 0)            return searchNode(target, startNode.right);        else if (target.compareTo(startNode.left.val) == 0)            return searchNode(target, startNode.left);        else            return null;    }    /*     * 判断一个二叉树是不是合法的二叉树的非递归遍历 采用中序遍历,并保存一个前驱节点,这样在每检查一个     * 节点的时候,就跟前驱节点对比,如果比前驱节点小(或者等于) 就表示不合法     */    public static boolean isValidBST(TreeNode root) {        Stack<TreeNode> stack = new Stack<TreeNode>();        // 设置前驱节点        TreeNode pre = null;        while (root != null || !stack.isEmpty()) {            while (root != null) { // 将当前节点,以及左子树一直入栈,循环结束时,root==null                stack.push(root);                root = root.left;            }            root = stack.pop();            // 比较并更新前驱,与普通遍历的区别就在下面四行            if (pre != null && root.val <= pre.val) {                return false;            }            pre = root;            root = root.right; // 访问右子树        }        return true;    }}
阅读全文
1 0
原创粉丝点击