Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fdfa05e

Browse files
committed
scala array argmax
1 parent 7efa748 commit fdfa05e

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

‎654_maximum_binary_tree.scala‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Solution {
2+
def constructMaximumBinaryTree(nums: Array[Int]): TreeNode = {
3+
val maxIdx = nums.zipWithIndex.maxBy(_._1)._2
4+
val root = TreeNode(nums(maxIdx))
5+
if (maxIdx == 0) root.left = null
6+
else root.left = constructMaximumBinaryTree(nums.slice(0, maxIdx))
7+
if (maxIdx == nums.length - 1) root.right = null
8+
else root.right = constructMaximumBinaryTree(nums.slice(maxIdx + 1, nums.length))
9+
root
10+
}
11+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /