Skip to content

Commit 1b61427

Browse files
authored
530. Minimum Absolute Difference in BST
1 parent 6e390df commit 1b61427

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Example:
3+
* var ti = TreeNode(5)
4+
* var v = ti.`val`
5+
* Definition for a binary tree node.
6+
* class TreeNode(var `val`: Int) {
7+
* var left: TreeNode? = null
8+
* var right: TreeNode? = null
9+
* }
10+
*/
11+
//https://leetcode.com/problems/binary-tree-right-side-view/description
12+
class Solution {
13+
fun rightSideView(root: TreeNode?): List<Int> {
14+
val result = mutableListOf<Int>()
15+
rightSideViewUtils(root, 0, result)
16+
return result
17+
}
18+
19+
fun rightSideViewUtils(node: TreeNode?, level: Int, list: MutableList<Int>){
20+
if(node == null) return
21+
if(list.getOrNull(level) == null){
22+
list.add(node.`val`)
23+
}
24+
rightSideViewUtils(node.right, level+1, list)
25+
rightSideViewUtils(node.left, level+1, list)
26+
}
27+
}

0 commit comments

Comments
 (0)