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 961553c

Browse files
committed
Find Largest Value in Each Tree Row
1 parent 4adb80d commit 961553c

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
};

0 commit comments

Comments
(0)

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