173. Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
Calling next() will return the next smallest number in the BST.
Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
173. Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree. 这个题constructor之类的, 不会写, 不知道什么原理 /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class BSTIterator { private Stack<TreeNode> stack; // 为啥是 private public BSTIterator(TreeNode root) { stack = new Stack<>(); //为啥在这new 一下? pushLeft(root); } /** @return whether we have a next smallest number */ public boolean hasNext() { return (!stack.isEmpty()); } /** @return the next smallest number */ public int next() { int curVal = 0; if(hasNext()){ TreeNode curNode = stack.pop(); curVal = curNode.val; if(curNode.right != null){ pushLeft(curNode.right); } } return curVal; //return curNode.val; // Line 37: error: cannot find symbol: variable curNode } private void pushLeft(TreeNode node){ while(node != null){ stack.push(node); node = node.left; } } } =========================== 写个BST的in-order iterator,要写的function有 next() 和 all(), all() return所有剩下的。 /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class BSTIterator { private Stack<TreeNode> stack; public BSTIterator(TreeNode root) { stack = new Stack<>(); pushLeft(root); } /** @return whether we have a next smallest number */ public boolean hasNext() { return !stack.isEmpty(); } /** @return the next smallest number */ public int next() { int curVal = 0; if(hasNext()){ TreeNode curNode = stack.pop(); curVal = curNode.val; if(curNode.right != null){ pushLeft(curNode.right); } } return curVal; //return curNode.val; // Line 37: error: cannot find symbol: variable curNode } public List<TreeNode> all(){ List<TreeNode> result = new ArrayList<>(); while(hasNext()){ result.add(next()); } return result; } private void pushLeft(TreeNode node){ while(node != null){ stack.push(node); node = node.left; } } } ============================== // preorder traversal // 面试怎么做? // 把 root 先放进 stack 里,pop stack, add it // to the list , 然后放 right , 然后放 left class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); if(root == null) return result; // null Stack<TreeNode> stack = new Stack<>(); stack.push(root); while(!stack.isEmpty()){ TreeNode cur = stack.pop(); result.add(cur.val); if(cur.right != null) stack.push(cur.right); if(cur.left != null) stack.push(cur.left); } return result; } } ============================== // preorder iterator // 得把这个要自己定义的东西 搞懂, 记住, 现在还不会定义 // public class PreorderIterator{ private Stack<TreeNode> stack; public PreorderIterator(TreeNode root){ stack = new Stack<TreeNode>(); if(root != null){ stack.push(root); } } @Override public boolean hasNext(){ return !stack.isEmpty(); } @Override public int next(){ if(!hasNext()){ throw new NoSuchElementException("no nodes left"); } TreeNode cur = stack.pop(); if(cur.right != null) stack.push(cur.right); if(cur.left != null) stack.push(cur.left); return cur.val; } } ======================================== // in order traversal class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); if(root == null) return result; Stack<TreeNode> stack = new Stack<>(); pushLeft(stack, root); while(!stack.isEmpty()){ TreeNode cur = stack.pop(); result.add(cur.val); if(cur.right != null){ pushLeft(stack, cur.right); } } return result; } private void pushLeft(Stack<TreeNode> stack, TreeNode node){ while(node != null){ stack.push(node); node = node.left; } } }
posted on 2018-08-09 17:11 猪猪🐷 阅读(85) 评论(0) 收藏 举报
浙公网安备 33010602011771号