Issue checking if binary tree is also binary search tree

前端 未结 4 1658
-上瘾入骨i
-上瘾入骨i 2021-01-19 04:12

I\'m trying to solve this problem but I\'m having some troubles:

In a binary search tree (BST):

  • The data value of every node in a
4条回答
  •  别那么骄傲
    2021-01-19 04:44

    Your recursion logic is not proper. I am giving here cpp logic. You may need to translate it to Java code.

    bool check(Node *root) {

    static Node *prev = NULL;
    
    if(root) {
    
        If(!check(root->left)) return false;
    
        If(prev != Null && prev->data > root->data) return false;
    
        Prev = root;
    
        return check(root->right);
    
    
    }
    
    return true;
    

    }

提交回复
热议问题