★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-rsnhzxmp-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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).node
For example:
Given binary tree [3,9,20,null,null,15,7]
,git
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:github
[ [15,7], [9,20], [3] ]
給定一個二叉樹,返回其節點值自底向上的層次遍歷。 (即按從葉子節點所在層到根節點所在的層,逐層從左向右遍歷)微信
例如:
給定二叉樹 [3,9,20,null,null,15,7]
,app
3 / \ 9 20 / \ 15 7
返回其自底向上的層次遍歷爲:spa
[ [15,7], [9,20], [3] ]
12ms
/** * Definition for a binary tree node. * public class TreeNode { * public var val: Int * public var left: TreeNode? * public var right: TreeNode? * public init(_ val: Int) { * self.val = val * self.left = nil * self.right = nil * } * } */ class Solution { func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { //遞歸方法 //當depth遞歸到上一層的個數,新建一個空層,繼續往裏面加數字。 var res:[[Int]] = [[Int]]() if root == nil {return res} var intList:[Int] = [Int]() levelOrder(root , 0 , &res) var temp:[Int] = [Int]() let num:Int = res.count-1 //交換第一位和最後一位 //交換第二位和倒數第二位...... for i in 0...num { temp = res[i] res[i] = res[num-i] res[num-i] = temp } return res.reversed() } func levelOrder(_ root: TreeNode?, _ depth:Int,_ re: inout [[Int]] ) { if root == nil {return} if (depth + 1) > re.count { re.append([Int]()) } re[depth].append(root!.val) levelOrder(root!.left,depth+1,&re) levelOrder(root!.right,depth+1,&re) } }
12mscode
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func levelOrderBottom (_ root: TreeNode?) -> [[Int]] { 16 var res = [[Int]]() 17 checkValuesOnLevel(&res, node: root, level: 0) 18 return res 19 } 20 21 func checkValuesOnLevel (_ res: inout [[Int]], node: TreeNode?, level: Int) { 22 guard let node = node else { return } 23 if level >= res.count { 24 res.insert ([], at: 0) 25 } 26 checkValuesOnLevel(&res, node: node.left, level: level + 1) 27 checkValuesOnLevel(&res, node: node.right, level: level + 1) 28 res[res.count - level - 1].append(node.val) 29 } 30 }
16mshtm
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { 16 guard let tNode = root else { 17 return [] 18 } 19 var result: [[Int]] = [] 20 var tempNodes: [TreeNode] = [tNode] 21 while !tempNodes.isEmpty { 22 let count = tempNodes.count 23 var data: [Int] = [] 24 for i in 0..<count { 25 let node = tempNodes[i] 26 data.append(node.val) 27 if let lNode = node.left { 28 tempNodes.append(lNode) 29 } 30 if let rNode = node.right { 31 tempNodes.append(rNode) 32 } 33 } 34 tempNodes.removeSubrange(Range.init(NSRange(location: 0, length: count))!) 35 result.append(data) 36 } 37 return result.reversed() 38 } 39 }
16msblog
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * public var val: Int 5 * public var left: TreeNode? 6 * public var right: TreeNode? 7 * public init(_ val: Int) { 8 * self.val = val 9 * self.left = nil 10 * self.right = nil 11 * } 12 * } 13 */ 14 class Solution { 15 func levelOrderBottom(_ root: TreeNode?) -> [[Int]] { 16 if root == nil { 17 return [] 18 } 19 20 var result = [[Int]]() 21 var queue = [TreeNode]() 22 queue.append(root!) 23 while !queue.isEmpty { 24 var count = queue.count 25 var temp = [Int]() 26 while count != 0 { 27 let node = queue.removeFirst() 28 temp.append(node.val) 29 if node.left != nil { 30 queue.append(node.left!) 31 } 32 if node.right != nil { 33 queue.append(node.right!) 34 } 35 count -= 1 36 } 37 result.append(temp) 38 } 39 40 return result.reversed() 41 } 42 }