首页 > 其他 > 详细

CC150 4.4

时间:2014-11-27 08:01:53      阅读:250      评论:0      收藏:0      [点我收藏+]

4.4 Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists).

List<List<Node>> build(Node root)
{
  List<List<Node>> toReturn = initList();
  build(root, 0, toReturn);
  return toReturn;
}

private void build(Node node, int dep, List<List<Node>> lists)
{
  if (node == null)
    return;
  
  List<Node> list = lists.get(dep);
  if (list == null) list = initList();
  list.add(node);
  
  build(node.left, dep + 1, lists);
  build(node.right, dep + 1, lists);
}


CC150 4.4

原文:http://7371901.blog.51cto.com/7361901/1583043

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