|
| 1 | +# 107. Binary Tree Level Order Traversal II |
| 2 | + |
| 3 | +- Difficulty: Easy. |
| 4 | +- Related Topics: Tree, Breadth-first Search. |
| 5 | +- Similar Questions: Binary Tree Level Order Traversal, Average of Levels in Binary Tree. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +Given a binary tree, return the *bottom-up level order* traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). |
| 10 | + |
| 11 | +For example: |
| 12 | +Given binary tree ```[3,9,20,null,null,15,7]```, |
| 13 | +``` |
| 14 | + 3 |
| 15 | + / \ |
| 16 | + 9 20 |
| 17 | + / \ |
| 18 | + 15 7 |
| 19 | +``` |
| 20 | + |
| 21 | +return its bottom-up level order traversal as: |
| 22 | +``` |
| 23 | +[ |
| 24 | + [15,7], |
| 25 | + [9,20], |
| 26 | + [3] |
| 27 | +] |
| 28 | +``` |
| 29 | + |
| 30 | +## Solution 1 |
| 31 | + |
| 32 | +```javascript |
| 33 | +/** |
| 34 | + * Definition for a binary tree node. |
| 35 | + * function TreeNode(val) { |
| 36 | + * this.val = val; |
| 37 | + * this.left = this.right = null; |
| 38 | + * } |
| 39 | + */ |
| 40 | +/** |
| 41 | + * @param {TreeNode} root |
| 42 | + * @return {number[][]} |
| 43 | + */ |
| 44 | +var levelOrderBottom = function(root) { |
| 45 | + var res = []; |
| 46 | + helper(root, 0, res); |
| 47 | + return res.reverse(); |
| 48 | +}; |
| 49 | + |
| 50 | +var helper = function (root, level, res) { |
| 51 | + if (!root) return; |
| 52 | + if (!res[level]) res[level] = []; |
| 53 | + res[level].push(root.val); |
| 54 | + helper(root.left, level + 1, res); |
| 55 | + helper(root.right, level + 1, res); |
| 56 | +}; |
| 57 | +``` |
| 58 | + |
| 59 | +**Explain:** |
| 60 | + |
| 61 | +nope. |
| 62 | + |
| 63 | +**Complexity:** |
| 64 | + |
| 65 | +* Time complexity : O(n). |
| 66 | +* Space complexity : O(n). |
| 67 | + |
| 68 | +## Solution 2 |
| 69 | + |
| 70 | +```javascript |
| 71 | +/** |
| 72 | + * Definition for a binary tree node. |
| 73 | + * function TreeNode(val) { |
| 74 | + * this.val = val; |
| 75 | + * this.left = this.right = null; |
| 76 | + * } |
| 77 | + */ |
| 78 | +/** |
| 79 | + * @param {TreeNode} root |
| 80 | + * @return {number[][]} |
| 81 | + */ |
| 82 | +var levelOrderBottom = function(root) { |
| 83 | + var res = []; |
| 84 | + helper(root, 0, res); |
| 85 | + return res; |
| 86 | +}; |
| 87 | + |
| 88 | +var helper = function (root, level, res) { |
| 89 | + if (!root) return; |
| 90 | + if (res.length < level + 1) res.unshift([]); |
| 91 | + res[res.length - level - 1].push(root.val); |
| 92 | + helper(root.left, level + 1, res); |
| 93 | + helper(root.right, level + 1, res); |
| 94 | +}; |
| 95 | +``` |
| 96 | + |
| 97 | +**Explain:** |
| 98 | + |
| 99 | +nope. |
| 100 | + |
| 101 | +**Complexity:** |
| 102 | + |
| 103 | +* Time complexity : O(n). |
| 104 | +* Space complexity : O(n). |
| 105 | + |
| 106 | +## Solution 3 |
| 107 | + |
| 108 | +```javascript |
| 109 | +/** |
| 110 | + * Definition for a binary tree node. |
| 111 | + * function TreeNode(val) { |
| 112 | + * this.val = val; |
| 113 | + * this.left = this.right = null; |
| 114 | + * } |
| 115 | + */ |
| 116 | +/** |
| 117 | + * @param {TreeNode} root |
| 118 | + * @return {number[][]} |
| 119 | + */ |
| 120 | +var levelOrderBottom = function(root) { |
| 121 | + var res = []; |
| 122 | + var stack = [[root, 0]]; |
| 123 | + var level = 0; |
| 124 | + var node = null; |
| 125 | + |
| 126 | + while (stack.length) { |
| 127 | + [node, level] = stack.pop(); |
| 128 | + if (node) { |
| 129 | + if (res.length < level + 1) res.unshift([]); |
| 130 | + res[res.length - level - 1].push(node.val); |
| 131 | + stack.push([node.right, level + 1]); |
| 132 | + stack.push([node.left, level + 1]); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return res; |
| 137 | +}; |
| 138 | +``` |
| 139 | + |
| 140 | +**Explain:** |
| 141 | + |
| 142 | +nope. |
| 143 | + |
| 144 | +**Complexity:** |
| 145 | + |
| 146 | +* Time complexity : O(n). |
| 147 | +* Space complexity : O(n). |
| 148 | + |
| 149 | +## Solution 4 |
| 150 | + |
| 151 | +```javascript |
| 152 | +/** |
| 153 | + * Definition for a binary tree node. |
| 154 | + * function TreeNode(val) { |
| 155 | + * this.val = val; |
| 156 | + * this.left = this.right = null; |
| 157 | + * } |
| 158 | + */ |
| 159 | +/** |
| 160 | + * @param {TreeNode} root |
| 161 | + * @return {number[][]} |
| 162 | + */ |
| 163 | +var levelOrderBottom = function(root) { |
| 164 | + var res = []; |
| 165 | + var queue = [[root, 0]]; |
| 166 | + var level = 0; |
| 167 | + var node = null; |
| 168 | + |
| 169 | + while (queue.length) { |
| 170 | + [node, level] = queue.shift(); |
| 171 | + if (node) { |
| 172 | + if (res.length < level + 1) res.unshift([]); |
| 173 | + res[res.length - level - 1].push(node.val); |
| 174 | + queue.push([node.left, level + 1]); |
| 175 | + queue.push([node.right, level + 1]); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return res; |
| 180 | +}; |
| 181 | +``` |
| 182 | + |
| 183 | +**Explain:** |
| 184 | + |
| 185 | +nope. |
| 186 | + |
| 187 | +**Complexity:** |
| 188 | + |
| 189 | +* Time complexity : O(n). |
| 190 | +* Space complexity : O(n). |
0 commit comments