首页 > 其他 > 详细

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

时间:2017-08-14 17:20:11      阅读:182      评论:0      收藏:0      [点我收藏+]

题目原文:

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

原文:http://www.cnblogs.com/evasean/p/7358665.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!