★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-siryexgy-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.node
Write a data structure CBTInserter
that is initialized with a complete binary tree and supports the following operations:git
CBTInserter(TreeNode root)
initializes the data structure on a given tree with head node root
;CBTInserter.insert(int v)
will insert a TreeNode
into the tree with value node.val = v
so that the tree remains complete, and returns the value of the parent of the inserted TreeNode
;CBTInserter.get_root()
will return the head node of the tree.Example 1:github
Input: inputs = ["CBTInserter","insert","get_root"], inputs = [[[1]],[2],[]] Output: [null,1,[1,2]]
Example 2:微信
Input: inputs = ["CBTInserter","insert","insert","get_root"], inputs = [[[1,2,3,4,5,6]],[7],[8],[]] Output: [null,3,4,[1,2,3,4,5,6,7,8]]
Note:數據結構
1
and 1000
nodes.CBTInserter.insert
is called at most 10000
times per test case.0
and 5000
.徹底二叉樹是每一層(除最後一層外)都是徹底填充(即,結點數達到最大)的,而且全部的結點都儘量地集中在左側。app
設計一個用徹底二叉樹初始化的數據結構 CBTInserter
,它支持如下幾種操做:測試
CBTInserter(TreeNode root)
使用頭結點爲 root
的給定樹初始化該數據結構;CBTInserter.insert(int v)
將 TreeNode
插入到存在值爲 node.val = v
的樹中以使其保持徹底二叉樹的狀態,並返回插入的 TreeNode
的父結點的值;CBTInserter.get_root()
將返回樹的頭結點。示例 1:spa
輸入:inputs = ["CBTInserter","insert","get_root"], inputs = [[[1]],[2],[]] 輸出:[null,1,[1,2]]
示例 2:設計
輸入:inputs = ["CBTInserter","insert","insert","get_root"], inputs = [[[1,2,3,4,5,6]],[7],[8],[]] 輸出:[null,3,4,[1,2,3,4,5,6,7,8]]
提示:
1
到 1000
個結點。CBTInserter.insert
操做 10000
次。0
到 5000
之間。68ms
1 class CBTInserter { 2 3 var root: TreeNode? 4 var nodeArray = [TreeNode]() 5 6 init(_ root: TreeNode?) { 7 self.root = root 8 var nextLevelStack = [TreeNode]() 9 if let node = root { 10 nextLevelStack.append(node) 11 } 12 13 while nextLevelStack.count > 0 { 14 let levelStack = nextLevelStack 15 nextLevelStack = [TreeNode]() 16 for node in levelStack { 17 nodeArray.append(node) 18 if let left = node.left { 19 nextLevelStack.append(left) 20 } 21 if let right = node.right { 22 nextLevelStack.append(right) 23 } 24 } 25 } 26 } 27 28 func insert(_ v: Int) -> Int { 29 let newNode = TreeNode(v) 30 let newIndex = 1 + nodeArray.count 31 if newIndex <= 1 { 32 root = newNode 33 nodeArray.append(newNode) 34 return 0 35 } 36 37 let parentIndex = newIndex / 2 38 let parentNode = nodeArray[parentIndex - 1] 39 if newIndex % 2 == 0 { 40 parentNode.left = newNode 41 } else { 42 parentNode.right = newNode 43 } 44 nodeArray.append(newNode) 45 return parentNode.val 46 } 47 48 func get_root() -> TreeNode? { 49 return root 50 } 51 }
72ms
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 15 class CBTInserter { 16 var arr: [TreeNode] = [] 17 18 init(_ root: TreeNode?) { 19 guard let root = root else { return } 20 let q = Queue<TreeNode>() 21 q.enqueue(root) 22 while !q.isEmpty { 23 var node = q.dequeue()! 24 arr.append(node) 25 if node.left != nil { 26 q.enqueue(node.left!) 27 } 28 if node.right != nil { 29 q.enqueue(node.right!) 30 } 31 } 32 } 33 34 func insert(_ v: Int) -> Int { 35 let node = TreeNode(v) 36 let index = arr.count 37 arr.append(node) 38 let parent = (index - 1)/2 39 if parent >= 0 { 40 let par = arr[parent] 41 if (2*parent + 1) == index { 42 par.left = node 43 } else { 44 par.right = node 45 } 46 return par.val 47 } else { 48 return 0 49 } 50 } 51 52 func get_root() -> TreeNode? { 53 return arr.first 54 } 55 } 56 57 public class Queue<T> { 58 fileprivate var array = [T?]() 59 fileprivate var head = 0 60 61 public var isEmpty: Bool { 62 return count == 0 63 } 64 65 public var count: Int { 66 return array.count - head 67 } 68 69 public func enqueue(_ element: T) { 70 array.append(element) 71 } 72 73 public func dequeue() -> T? { 74 guard head < array.count, let element = array[head] else { return nil } 75 76 array[head] = nil 77 head += 1 78 79 let percentage = Double(head)/Double(array.count) 80 if array.count > 50 && percentage > 0.25 { 81 array.removeFirst(head) 82 head = 0 83 } 84 85 return element 86 } 87 88 public var front: T? { 89 if isEmpty { 90 return nil 91 } else { 92 return array[head] 93 } 94 } 95 } 96 97 /** 98 * Your CBTInserter object will be instantiated and called as such: 99 * let obj = CBTInserter(root) 100 * let ret_1: Int = obj.insert(v) 101 * let ret_2: TreeNode? = obj.get_root() 102 */ 103
76ms
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 15 class CBTInserter { 16 var root: TreeNode? 17 var total = 0 18 init(_ root: TreeNode?) { 19 self.root = root 20 count(root) 21 //print(total) 22 } 23 24 func count(_ r: TreeNode?) { 25 guard let r = r else { 26 return 27 } 28 total += 1 29 count(r.left) 30 count(r.right) 31 } 32 33 func insert(_ v: Int) -> Int { 34 total += 1 35 let bs = String(total, radix: 2) 36 //print(bs) 37 var p = root 38 for i in 1 ..< bs.count { 39 let b = bs[bs.index(bs.startIndex, offsetBy: i)] 40 let q = b == "0" ? p?.left : p?.right 41 if q == nil { 42 if b == "0" { 43 p?.left = TreeNode(v) 44 } 45 else { 46 p?.right = TreeNode(v) 47 } 48 return p!.val 49 } 50 p = q 51 } 52 return -1 53 } 54 55 func get_root() -> TreeNode? { 56 return root 57 } 58 } 59 60 /** 61 * Your CBTInserter object will be instantiated and called as such: 62 * let obj = CBTInserter(root) 63 * let ret_1: Int = obj.insert(v) 64 * let ret_2: TreeNode? = obj.get_root() 65 */
80ms
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 15 class CBTInserter { 16 var root:TreeNode? 17 var deque:[TreeNode?] = [TreeNode?]() 18 init(_ root: TreeNode?) { 19 self.root = root 20 var queue:[TreeNode?] = [TreeNode?]() 21 queue.append(root) 22 23 // BFS to populate deque 24 while(!queue.isEmpty) 25 { 26 var node:TreeNode? = queue.removeFirst() 27 if node?.left == nil || node?.right == nil 28 { 29 deque.append(node) 30 } 31 32 if node?.left != nil 33 { 34 queue.append(node?.left) 35 } 36 if node?.right != nil 37 { 38 queue.append(node?.right) 39 } 40 } 41 } 42 43 func insert(_ v: Int) -> Int { 44 var node:TreeNode? = deque.first! 45 deque.append(TreeNode(v)) 46 if node?.left == nil 47 { 48 node?.left = deque.last! 49 } 50 else 51 { 52 node?.right = deque.last! 53 deque.removeFirst() 54 } 55 return node!.val 56 } 57 58 func get_root() -> TreeNode? { 59 return root 60 } 61 }