Pergunta de entrevista da empresa X

Determine if a binary search tree is valid or not.

Respostas da entrevista

Sigiloso

18 de ago. de 2014

BOOL isBST(NODE root) { static NODE previous = NULL; if(root) { root = root->left; BOOL temp = isBST(root); if(!temp) return FALSE; if(previous != NULL && root->data data) return FALSE; previous = root; isBST(!root->right) return FALSE; } else return TRUE; }

Sigiloso

16 de out. de 2014

Perform an in-order traversal of the tree. If the node values are sorted (ascending), it is a BST.

Sigiloso

3 de abr. de 2016

boolean isValidBST(TreeNode root) { if(root == null) return true; return helper(root, Integer.MIN_VALUE, Integer.MAX_VALUE); } boolean helper(TreeNode root, int min, int max) { if(root == null) return true; if(root.val == Integer.MAX_VALUE) return false; else return(helper(root.left, min, root.val) && helper(root.root,root.val, max); }

Sigiloso

18 de ago. de 2014

BOOL isBST(NODE root) { static NODE previous = NULL; if(root) { root = root->left; BOOL temp = isBST(root); if(!temp) return FALSE; if(previous != NULL && root->data data) return FALSE; previous = root; return isBST (root->right); } else return TRUE; }