We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Practice
  • Certification
  • Compete
  • Career Fair
  • Hiring developers?
  1. Practice
  2. Tutorials
  3. Cracking the Coding Interview
  4. Trees: Is This a Binary Search Tree?
  5. Discussions

Trees: Is This a Binary Search Tree?

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • Jstorm99 4 years ago+ 0 comments

    You can do it like this ( if u say this is clean :p)

    boolean checkBST(Node root) {
            ArrayList<Integer> arr=new ArrayList<Integer>();
            inorder(root,arr);
            int flag=0;
            for(int i=1;i<arr.size();i++)
    			if(arr.get(i)>arr.get(i-1))
    				continue;
    			else
    			    return false;
            return true;
        }
        
        void inorder(Node root,ArrayList<Integer> ar)
        {
            if(root==null)return;
            inorder(root.left,ar);
            ar.add(root.data);
            inorder(root.right,ar);
        }
    
    11|
    ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature