【问题标题】:Java - Including BSTJava - 包括 BST
【发布时间】:2016-10-17 20:25:56
【问题描述】:

我有这个 BST 问题,我正试图在 java 中解决,但我不知道为什么它不起作用。 问题来了:

  • 二叉搜索树 (BST) 是一棵二叉树,其中每个 节点大于或等于该节点的所有节点中的值 左子树并且小于该子树中所有节点的值 节点的右子树。

    编写一个函数来检查给定的二叉搜索树是否包含 给定值。

    例如,对于以下树:

    n1(值:1,左:null,右:null) n2(值:2,左:n1,右: n3) n3 (值: 3, 左: null, 右: null) 调用 contains(n2, 3) 应该返回 true,因为根在 n2 的树包含数字 3。

这就是我试图解决的方法:

    class Node {
    public int value;
    public Node left, right;

    public Node(int value, Node left, Node right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
}

public class BinarySearchTree {
    public static boolean contains(Node root, int value) {
       if(root.value == value)
           return true;

       else if(value < root.value){
           if(root.left == null)
               return false;
           contains(root.left, value);
       }
       else if(value > root.value){
           if(root.right == null)
               return false;
           contains(root.right, value);
       }

        return false;
    }


    public static void main(String[] args) {
        Node n1 = new Node(1, null, null);
        Node n3 = new Node(3, null, null);
        Node n2 = new Node(2, n1, n3);

        System.out.println(contains(n2, 3));
    }
}

这应该返回 true,但它没有......提前谢谢你。

【问题讨论】:

  • 你需要说return contains(...),而不仅仅是调用方法。现在,您正在丢弃递归调用的结果,并最终到达方法末尾的return false;。
  • 当然这就是原因……天哪,我感觉自己很愚蠢。非常感谢。

标签: java


【解决方案1】:

return contains(...) 而不是仅仅为那些递归调用调用contains(...),这样你就不会在调用时丢失值

【讨论】:

    【解决方案2】:
    public class BinarySearchTree {
        public static boolean contains(Node root, int value) {
           if(root.value==value)
               return true;
            else if(value<root.value && root.left!=null)
                return contains(root.left,value);
                else if(value> root.value && root.right!=null)
                    return contains(root.right,value);
                    else return false;
        }
    

    【讨论】:

    • 解释你的答案
    【解决方案3】:

    我写了一个 c++ 版本。好像理解起来比较清楚。

    #include <stdexcept>
    #include <string>
    #include <iostream>
    #include <algorithm>
    
    class Node
    {
    public:
       Node(int value, Node* left, Node* right)
       {
          this->value = value;
          this->left = left;
          this->right = right;
       }
    
       int getValue() const
       {
          return value;
       }
    
       Node* getLeft() const
       {
          return left;
       }
    
       Node* getRight() const
       {
          return right;
       }
    
    private:
       int value;
       Node* left;
       Node* right;
    };
    class BinarySearchTree
    {
    public:
    
       static bool isValidBST( Node& root)
       {
    
        if ((root.getLeft())&&(root.getValue()<=(root.getLeft()->getValue())))
            return false;
        if ((root.getRight())&&(root.getValue()>=(root.getRight()->getValue())))
        {
            return false;
        }
        if (root.getRight())
        {
            if(!isValidBST(*(root.getRight()))) return false;
        }
        if(root.getLeft())
        {
            if(!isValidBST(*(root.getLeft()))) return false;
        }
    
        return true;
    
        }
    };
    
    
    #ifndef RunTests
    int main(int argc, const char* argv[])
    {
        Node n7(7, NULL, NULL);
        Node n10(11, NULL, NULL);
        Node n2(2, NULL, NULL);
        Node n5(5, NULL, NULL);
        Node n4(4, &n2, &n5);
        Node n9(9, &n7, &n10);
        Node n6(6, &n4, &n9);
        
        std::cout << BinarySearchTree::isValidBST(n6);
    
        return 0;
    }
    #endif
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 2011-03-07
      • 2021-07-14
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多