Skip to content

Commit 2d7ce08

Browse files
author
Partho Biswas
committed
199_Binary_Tree_Right_Side_View
1 parent 0db88a5 commit 2d7ce08

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ Check this [golden](https://tinyurl.com/ujopecz) post.
432432
|14| **[979. Distribute Coins in Binary Tree](https://leetcode.com/problems/distribute-coins-in-binary-tree/)**| [Python](https://tinyurl.com/wu6rdaw/979_Distribute_Coins_in_Binary_Tree.py)| [Art 1](https://tinyurl.com/rugoa38), [Art 2](https://tinyurl.com/whkdbl4), [Art 3](https://tinyurl.com/s62fws7) | Medium | **Very important. Postorder DFS** |
433433
|15| [116. Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node/)| [Python](https://tinyurl.com/wu6rdaw/116_Populating_Next_Right_Pointers_in_Each_Node.py)| [Art 1](https://tinyurl.com/t6p8kjb), [Art 2](https://tinyurl.com/ujzrhkj), **[Art 3](https://tinyurl.com/unrow77)** | Medium | BFS/DFS, Level order traversal |
434434
|16| [117. Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)| [Python](https://tinyurl.com/wu6rdaw/117_Populating_Next_Right_Pointers_in_Each_Node_II.py)| [Vid 1](https://tinyurl.com/rfjhzj3), [Vid 2](https://tinyurl.com/ryghj42) | Medium | **Very tricky, check again** |
435-
|17| [199. Binary Tree Right Side View](https://tinyurl.com/y65vxqj2)| [Python](https://tinyurl.com/wu6rdaw/199_Binary_Tree_Right_Side_View.py)| --- | Medium | --- |
435+
|17| [199. Binary Tree Right Side View](https://tinyurl.com/y65vxqj2)| [Python](https://tinyurl.com/wu6rdaw/199_Binary_Tree_Right_Side_View.py), [Swift](https://tinyurl.com/wuja3c4/199_Binary_Tree_Right_Side_View.swift) | --- | Medium | --- |
436436
|18| **[863. All Nodes Distance K in Binary Tree](https://tinyurl.com/w5dmj3o)**| [Python](https://tinyurl.com/wu6rdaw/863_All_Nodes_Distance_K_in_Binary_Tree.py)| [Vid 1](https://tinyurl.com/tn4ncqy), [Art 1](https://tinyurl.com/t3ozct3), [Art 2](https://tinyurl.com/rvbhznn) | Medium | A combination of graph and tree. TODO: Check again. Very important |
437437
|19| [301. Remove Invalid Parentheses](https://tinyurl.com/w9lfsgt) | [Python](https://tinyurl.com/wu6rdaw/301_Remove_Invalid_Parentheses.py)| [Vid 1](https://tinyurl.com/rbp6xa7), [Vid 2](https://tinyurl.com/sjk44du), [Art 1](https://tinyurl.com/tcglxfp) | Hard | TODO: Not done, Check again. Very important. BFS and DFS |
438438
|20| [112. Path Sum](https://tinyurl.com/rg6m7ua) | [Python](https://tinyurl.com/wu6rdaw/112_Path_Sum.py)| [Art 1](https://tinyurl.com/t2dggse) | Easy | --- |
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Foundation
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* public var val: Int
6+
* public var left: TreeNode?
7+
* public var right: TreeNode?
8+
* public init() { self.val = 0; self.left = nil; self.right = nil; }
9+
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
10+
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
11+
* self.val = val
12+
* self.left = left
13+
* self.right = right
14+
* }
15+
* }
16+
*/
17+
class Solution {
18+
func rightSideView(_ root: TreeNode?) -> [Int] {
19+
guard var root = root else {
20+
return []
21+
}
22+
var rightView = [Int]()
23+
var (maxDepthSoFar, currentDepth) = (-1, -1)
24+
rightSideViewDFSHelper(root, currentDepth + 1, &maxDepthSoFar, &rightView)
25+
return rightView
26+
}
27+
28+
private func rightSideViewDFSHelper(_ root: TreeNode?, _ currentDepth: Int, _ maxDepthSoFar: inout Int, _ rightView: inout [Int]) {
29+
guard var root = root else {
30+
return
31+
}
32+
if currentDepth > maxDepthSoFar {
33+
rightView.append(root.val)
34+
maxDepthSoFar = currentDepth
35+
}
36+
rightSideViewDFSHelper(root.right, currentDepth + 1, &maxDepthSoFar, &rightView)
37+
rightSideViewDFSHelper(root.left, currentDepth + 1, &maxDepthSoFar, &rightView)
38+
}
39+
}

0 commit comments

Comments
 (0)