-
Notifications
You must be signed in to change notification settings - Fork 0
/
107.二叉树的层序遍历-ii.js
37 lines (35 loc) · 940 Bytes
/
107.二叉树的层序遍历-ii.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
* @lc app=leetcode.cn id=107 lang=javascript
*
* [107] 二叉树的层序遍历 II
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
const __levelOrderButton = (root, res, index) => {
if (!root) return;
res[index] ? res[index].push(root.val) : (res[index] = [root.val]);
__levelOrderButton(root.left, res, index + 1);
__levelOrderButton(root.right, res, index + 1);
};
/**
* @param {TreeNode} root
* @return {number[][]}
*/
var levelOrderBottom = function (root) {
let res = [];
__levelOrderButton(root, res, 0);
for (let i = 0, j = res.length - 1; i < j; i++, j--) {
let temp = res[i];
res[i] = res[j];
res[j] = temp;
}
return res;
};
// @lc code=end