[Random Coding] Tree Iterator
来源:互联网 发布:icloud照片下载到mac 编辑:程序博客网 时间:2024/05/02 04:55
package RandomPractice;import java.util.NoSuchElementException;import util.*;public class TreeIterator {private BTNode next;public TreeIterator(BTNode root){next = root;if(next == null)return;while(next.left != null)next = next.left;}public boolean hasNext(){return next != null;}public BTNode next(){if(!hasNext())throw new NoSuchElementException();BTNode result = next;if(next.right != null){next = next.right;while(next.left != null)next = next.left;} else{while(true){if(next.parent == null){next = null;break;} if(next.parent.left == next){next = next.parent;break;}next = next.parent;}}return result;}public static void main(String[] args){BTNode root = new BTNode(4);root.left = new BTNode(2);root.right = new BTNode(5);root.left.parent = root;root.right.parent = root;root.left.left = new BTNode(1);root.left.right = new BTNode(3);root.left.left.parent = root.left;root.left.right.parent = root.left;TreeIterator it = new TreeIterator(root);while(it.hasNext())System.out.println(it.next().value);}}
0 0
- [Random Coding] Tree Iterator
- [Random Coding] De/Serialization Binary Tree
- [Random Coding] De/Serialization of Binary Search Tree
- [Random Coding]QueueWithMin
- [Random Coding]StackWithMin
- [Random Coding]Topological Sorting
- Random Access Iterator
- [Random Coding]Interval Related Questions
- vp8 tree coding
- coding tree unit解析
- Binary tree inorder iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- Binary Search Tree Iterator
- [Leetcode] Binary Tree Preorder Traversal
- 我还年轻——献给37岁的自己
- [RandomCoding]SameLeafNode
- C# 调用dll 封送结构体 结构体数组
- windows环境下下载android源码
- [Random Coding] Tree Iterator
- 有向无环图的一些特性简介
- mvc4项目使用ueditor编辑器
- [Random Coding] De/Serialization Binary Tree
- [Random Coding] De/Serialization of Binary Search Tree
- [Leetcode] - Max Points on a Line
- 《黑马程序员》 高新技术第二天学习之枚举的入门和枚举的构造函数的调用
- 【静静的花开】有感:杂谈春天来
- java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file