Skip to content

Commit 00d87af

Browse files
committed
Solution of Binary Tree Iterator and Right side of Binary Tree
1 parent ffce373 commit 00d87af

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

Diff for: 100-200q/173.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'''
2+
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
3+
4+
Calling next() will return the next smallest number in the BST.
5+
6+
Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
7+
'''
8+
9+
10+
# Definition for a binary tree node
11+
# class TreeNode(object):
12+
# def __init__(self, x):
13+
# self.val = x
14+
# self.left = None
15+
# self.right = None
16+
17+
class BSTIterator(object):
18+
def __init__(self, root):
19+
"""
20+
:type root: TreeNode
21+
"""
22+
self.stack = []
23+
while root:
24+
self.stack.append(root)
25+
root = root.left
26+
27+
28+
def hasNext(self):
29+
"""
30+
:rtype: bool
31+
"""
32+
return self.stack
33+
34+
def next(self):
35+
"""
36+
:rtype: int
37+
"""
38+
node = self.stack.pop()
39+
new_node = node.right
40+
while new_node:
41+
self.stack.append(new_node)
42+
new_node = new_node.left
43+
return node.val
44+
45+
# Your BSTIterator will be called like this:
46+
# i, v = BSTIterator(root), []
47+
# while i.hasNext(): v.append(i.next())

Diff for: 100-200q/199.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'''
2+
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
3+
4+
Example:
5+
6+
Input: [1,2,3,null,5,null,4]
7+
Output: [1, 3, 4]
8+
Explanation:
9+
10+
1 <---
11+
/ \
12+
2 3 <---
13+
\ \
14+
5 4 <---
15+
'''
16+
17+
# Definition for a binary tree node.
18+
# class TreeNode(object):
19+
# def __init__(self, x):
20+
# self.val = x
21+
# self.left = None
22+
# self.right = None
23+
24+
class Solution(object):
25+
def rightSideView(self, root):
26+
"""
27+
:type root: TreeNode
28+
:rtype: List[int]
29+
"""
30+
if not root:
31+
return []
32+
33+
stack, node_depth = [(root, 0)], {}
34+
35+
while stack:
36+
node, depth = stack.pop(0)
37+
if depth not in node_depth:
38+
node_depth[depth] = node.val
39+
40+
if node.right:
41+
stack.append((node.right, depth+1))
42+
if node.left:
43+
stack.append((node.left, depth+1))
44+
return node_depth.values()

Diff for: README.md

+2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
5151
##### [Problems 100-200](./100-200q/)
5252
| # | Title | Solution | Difficulty |
5353
|---| ----- | -------- | ---------- |
54+
|199|[Binary Tree Right Side View](https://leetcode.com/problems/binary-tree-right-side-view)|[Python](./100-200q/199.py)|Medium|
5455
|191|[Number of 1 Bits](https://leetcode.com/problems/number-of-1-bits/)| [Python](./100-200q/191.py)|Easy|
5556
|179|[Largest Number](https://leetcode.com/problems/largest-number/) | [Python](./100-200q/179.py)|Medium|
57+
|173|[Binary Search Tree Iterator](https://leetcode.com/problems/binary-search-tree-iterator)|[Python](./100-200q/173.py)|Medium|
5658
|162|[Find Peak Element](https://leetcode.com/problems/find-peak-element/) | [Python](./100-200q/162.py)|Medium|
5759
|160|[Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [Python](./100-200q/160.py)|Easy|
5860
|155|[Min Stack](https://leetcode.com/problems/min-stack/)| [Python](./100-200q/155.py)|Easy|

0 commit comments

Comments
 (0)