[Golang][Leetcode][BinaryTree]刷題系列-515-Find Largest Value in Each Tree Row

Contents
515. Find Largest Value in Each Tree Row
Level : Medium
原題連結 : Click
題目 :
Given the root of a binary tree, return an array of the largest value in each row of the tree (0-indexed).
Example :
Note
Example 1:
Input: root = [1,3,2,5,3,null,9] Output: [1,3,9]
Example 2:
Input: root = [1,2,3] Output: [1,3]
Example 3:
Input: root = [1] Output: [1]
Example 4:
Input: root = [1,null,2] Output: [1,2]
Example 5:
Input: root = [] Output: []
解題思路 :
- 這題是level order traversal的變化題,一樣可以用queue來解決,可以參考 leetcode102題解
- 這類題目解法都差不多,需要要每個level 最大的value取出來就ok了!
Queue解法
- time complexity: O(n) , space complexity: O(n)
Runtime: 4 ms, faster than 98.39% of Go online submissions for Find Largest Value in Each Tree Row.
|
|
最後祝福努力認真的各位 “All your dream of are hidden in your daily life” 我們峰頂見!!!