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

笔试题:
判断给定的二叉树是否为二分查找树。假设树的每个节点都是以整数为键值,且不同节点值不相同。
返回结果:是二分查找树返回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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值