Binary Tree Right Side View
LeetCode 199 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ
Right-first DFS; record the first node per level.
நான் Binary Tree Right Side View (LeetCode 199) ஐ இப்படித் தீர்த்தேன் — BFS / Level Order பேட்டர்னின் ஒரு நடுத்தரம் பிராப்ளம். கீழே: intuition, படிப்படியான approach, என் accepted Python கோட், animated test cases மற்றும் line-by-line debugger.
கீழேயுள்ள விளக்கம் ஆங்கிலத்தில் உள்ளது.
Intuition
Do a DFS that visits the right child before the left. The first node you reach at each depth is the one visible from the right.
Approach
- 1
dfs(node, level): None → return.
- 2
If this level has no entry yet, record node.val.
- 3
Recurse right, then left, with level + 1.
நான் எப்படித் தீர்த்தேன்
Right-first DFSPythonO(n) time · O(h) space
class Solution:
def rightSideView(self, root):
ds = []
def dfs(node, level):
if node is None:
return
if len(ds) == level:
ds.append(node.val)
dfs(node.right, level + 1)
dfs(node.left, level + 1)
dfs(root, 0)
return dsBinary Tree Right Side View — அடிக்கடி கேட்கப்படும் கேள்விகள்
Binary Tree Right Side View பின்னாலுள்ள intuition என்ன?
Do a DFS that visits the right child before the left. The first node you reach at each depth is the one visible from the right.
Binary Tree Right Side View (LeetCode 199) ஐ எப்படித் தீர்ப்பது?
1. dfs(node, level): None → return. 2. If this level has no entry yet, record node.val. 3. Recurse right, then left, with level + 1.
Binary Tree Right Side View இன் time மற்றும் space complexity என்ன?
Time: O(n). Space: O(h). (Accepted approach: Right-first DFS.)
Binary Tree Right Side View இல் பொதுவான தவறுகள் என்ன?
A left-only branch deeper than the right side is still visible.