LeetCode – Refresh – Binary Tree Iterator

It is similar to use stack for BST inorder traversal.

So do the same thing :

1. Get stack to store right branchs (include current node).

2. When you pop the top node, remember to push all the right sub branches again. Otherwise, it will cause right‘s left branches missed from result

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class BSTIterator {
11 private:
12     stack<TreeNode *> s;
13 public:
14     void initialNodes(TreeNode *root) {
15         while (root) {
16             s.push(root);
17             root = root->left;
18         }
19     }
20     BSTIterator(TreeNode *root) {
21         initialNodes(root);
22     }
23
24     /** @return whether we have a next smallest number */
25     bool hasNext() {
26         return !s.empty();
27     }
28
29     /** @return the next smallest number */
30     int next() {
31         TreeNode *tmp = s.top();
32         s.pop();
33         if (tmp->right) {
34             initialNodes(tmp->right);
35         }
36         return tmp->val;
37     }
38 };
39
40 /**
41  * Your BSTIterator will be called like this:
42  * BSTIterator i = BSTIterator(root);
43  * while (i.hasNext()) cout << i.next();
44  */
时间: 2024-08-08 13:41:50

LeetCode – Refresh – Binary Tree Iterator的相关文章

LeetCode – Refresh – Binary Tree Inorder Traversal

There are three methods to do it: 1. recursive(use memory stack): (Time O(n), Space O(logn) 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), rig

LeetCode – Refresh – Binary Tree Post Order Traversal

Still 3 methods: Same with inorder, but post order is a little different. We need to treat it as reverse result of preorder. So we go to right branch first, then go back to left branch 1. recursive (use memory stack); 1 /** 2 * Definition for binary

LeetCode – Refresh – Binary Tree Level Order Traversal

Basic question. Use a queue to store Node, use two numbers to record current level node numbers and next level node numbers. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode

LeetCode – Refresh – Binary Tree Pre Order Traversal

Same with inorder. 1. recursive: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 v

LeetCode – Refresh – Binary Tree Maximum Path Sum

Use lMax get the maximum value of a sub SINGLE branch (left branch or right branch or just current node). Use gMax get the maximum value of the whole sub branch (include current node). So gMax need to pass by reference. 1 /** 2 * Definition for binar

LeetCode – Refresh – Binary Tree Level Order Traversal ii

This question is almost same as the previous one. I just use a stack (another vector) of vector<int> to store the level result. Then reverse it. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode

LeetCode – Refresh – Binary Tree Upside Down

Recursive method can be unstand easily: 1. Assume we get the sub node with this transition. So we need to make the current node. 2. As the symmetic, the finished sub node will replace the position of current node. 3. old current->left = parent ->rig

LeetCode – Refresh – Binary Tree Zigzag Level Order Traversal

Made a stupid bug....... When reverse the vector, stop it at mid. Otherwise, it will swap back...... Mark!!!!!!!! 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : v

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur