Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST

Posted evasean

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST相关的知识,希望对你有一定的参考价值。

题目原文:

Given a binary tree where each  ???????? contains a key, determine whether it is a binary search tree. Use extra space proportional to the height of the tree.

分析:

就是递归遍历BST,将每个节点x分别与x.left和x.right比大小。

 1 public boolean isBST(BST<Key,Value> bst){
 2     return isBST(root);
 3 }
 4 private boolean isBST(Node x){
 5     if(x.left.key.compareTo(x.key) != -1) return false;
 6     if(x.key.compareTo(x.right.key)!= -1) return false;
 7     if(x.left != null ) return isBST(x.left);
 8     if(x.right != null ) return isBST(x.right);
 9     return true;
10 }

 

以上是关于Coursera Algorithms week4 基础标签表 练习测验:Check if a binary tree is a BST的主要内容,如果未能解决你的问题,请参考以下文章

text Coursera Week4编程作业2

text Coursera Week4编程作业1

Coursera TensorFlow 基础课程-week4

Coursera TensorFlow 基础课程-week4

Coursera TensorFlow 基础课程-week4

吴恩达-coursera-机器学习-week4