首页 > 代码库 > CC150 4.5
CC150 4.5
4.5 Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where each node has a link to its parent.
// BST. class Node { Node parent; Node left; Node right; } Node min(Node node) { if (node == null) return null; while(node.left != null) node = node.left; return node; } // in-order ? Node next(Node node) { if (node == null) return null; if (node.right != null) { Node toReturn = node.right; while (toReturn.left != null) { toReturn = toReturn.left; } return toReturn; } while( node != null) { if (node == node.parent.left) return node.parent; else node = node.parent; } return null; }
CC150 4.5
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。