20150708 lintcode 总结 Binary Search Tree Iterator

原创 2015年07月08日 11:09:18

Binary Search Tree Iterator


Design an iterator over a binary search tree with the following rules:

  • Elements are visited in ascending order (i.e. an in-order traversal)
  • next() and hasNext() queries run in O(1) time in average.Example

For the following binary search tree, in-order traversal by using iterator is [1, 6, 10, 11, 12]

   10
 /    \
1      11
 \       \
  6       12
Challenge

Extra memory usage O(h), h is the height of the tree.

Super Star: Extra memory usage O(1)


/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 * Example of iterate a tree:
 * Solution iterator = new Solution(root);
 * while (iterator.hasNext()) {
 *    TreeNode node = iterator.next();
 *    do something for node
 * } 
 */
public class Solution {
	public Stack myStack = new Stack();
	public TreeNode current;
    //@param root: The root of binary tree.
    public Solution(TreeNode root) {
        // write your code here
    	current = root;
    	return;
    }

    //@return: True if there has next node, or false
    public boolean hasNext() {
        // write your code here
    	return (current!=null || !myStack.isEmpty());
    }
    
    //@return: return next node
    public TreeNode next() {
        // write your code here
    	while(current!=null){
    		myStack.push(current);
    		current = current.left;
    	}   	
    	TreeNode res = (TreeNode) myStack.pop();
    	current = res.right;
       	return res;
    }
}

核心:TreeNode next()

主要功能一直找出最小的那个node:

1. 如果current != null,一直找left.left.left.....就是所求结果; 

2. 如果current == null 并且current是 current.parent的left node时,current.parent 就是所求结果,也就是myStack最上面的node;

3. 如果current == null 并且current是 current.parent的right node时, current.parent已经被pop了,current.parent.parent是所求结果,也是myStack最上面的node。

第一个while loop非常巧妙:当current不等于null时,一直查询left node,并把沿途left node存入myStack,一直到最小的node的left node (值为null),然后再从stack里面读出来上一个left node,虽然可以用while(current.left!=null)来省略两步(current=current.left 和 current=myStack.pop() ), 但这样的话current == null的情况要多写一个if语句。while(curerent != null) 包括了 if(current == null) 的情况。 之后的从myStack读出来是两个情况都要做的下一步。

current 为结果时,再从以 current.right 为 root 的 tree 中找 next(), 所以最后要 current = current.right 


相关文章推荐

20150708 lintcode 总结 Binary Tree Maximum Path Sum Show result *****

Medium Binary Tree Maximum Path Sum Show result  23% Accepted Given a binary tree, find the ...

【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连

maxtree: http://lintcode.com/submission/60239/ 九章算法中有讲到, 对每个节点,找到离他最近且比它大的左右两个节点中较小的那个,作为他的父节点。 其中,...

[LeetCode]problem 173. Binary Search Tree Iterator

TAG中序遍历二叉查找树得到一个递增序列二叉查找树题目链接方法这道题挺有意思的,在二叉查找树的基础上建立一个迭代器,要求hasnext和next操作要是平均O(1)的,且最大空间消耗是O(h),h是树...

LeetCode - 173. Binary Search Tree Iterator

要求实现Binary Search Tree的hasNext()和next()操作,最小的元素自然是从root开始一直找左子树左子树直到为空,这是我们找到了我们需要的第一个元素,但是接下来的元素怎么办...

Binary Search Tree Iterator

Binary Search Tree Iterator

173. Binary Search Tree Iterator

题目:Binary Search Tree Iterator原题链接:https://leetcode.com/problems/binary-search-tree-iterator/ Imple...

Leetcode: Binary Search Tree Iterator

题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with t...

[C++]LeetCode: 93 Binary Search Tree Iterator (经典题,非递归的中序遍历)

题目: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with...

leetcode_173_Binary Search Tree Iterator

思路: 这道题想了好久,知道用中序遍历来解决,用一个list将遍历的元素存储起来一下就解决了,但是空间复杂度不行。具体怎么解决,如何控制程序的终止困扰了我好久。知道我想起来至多用O(h) memor...

[leetcode 173] Binary Search Tree Iterator-------为二叉搜索树实现迭代器功能

Question: Implement an iterator over a binary search tree (BST). Your iterator will be initiali...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:20150708 lintcode 总结 Binary Search Tree Iterator
举报原因:
原因补充:

(最多只允许输入30个字)