File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Algorithms/Find Largest Value in Each Tree Row Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ // Source : https://leetcode.com/problems/find-largest-value-in-each-tree-row/
2+ // Author : Han Zichi
3+ // Date : 2016-02-17
4+
5+ /**
6+ * Definition for a binary tree node.
7+ * function TreeNode(val) {
8+ * this.val = val;
9+ * this.left = this.right = null;
10+ * }
11+ */
12+ /**
13+ * @param {TreeNode } root
14+ * @return {number[] }
15+ */
16+ var largestValues = function ( root ) {
17+ let maxn = [ ] ;
18+
19+ let dfs = ( node , step ) => {
20+ if ( ! node ) return ;
21+ if ( maxn [ step ] === void 0 )
22+ maxn [ step ] = node . val ;
23+ else
24+ maxn [ step ] = Math . max ( node . val , maxn [ step ] ) ;
25+ dfs ( node . left , step + 1 ) ;
26+ dfs ( node . right , step + 1 ) ;
27+ } ;
28+
29+ dfs ( root , 0 ) ;
30+ return maxn ;
31+ } ;
You can’t perform that action at this time.
0 commit comments