码迷,mamicode.com
首页 > 其他好文 > 详细

Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space

时间:2017-08-14 19:00:13      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:pre   constant   orm   void   const   algo   private   html   use   

题目原文:

Design an algorithm to perform an inorder traversal of a binary search tree using only a constant amount of extra space.

 

 1 public void traverse(BST<Key,Value> bst) {
 2     traverse(bst.root.left, bst.root);
 3 }
 4 
 5 private void traverse(Node current, Node parent) {
 6     while (current != null) {
 7         if (parent != null) {
 8             parent.left = current.right;
 9             current.right = parent;
10         }
11         if (current.left != null) {
12             parent = current;
13             current = current.left;
14         } else {
15             System.out.println(current.key);
16             current = current.right;
17             parent = null;
18         }
19     }
20 }

 

Coursera Algorithms week4 基础标签表 练习测验:Inorder traversal with constant extra space

标签:pre   constant   orm   void   const   algo   private   html   use   

原文地址:http://www.cnblogs.com/evasean/p/7359235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!