在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given the root of a binary tree, then value The adding rule is: given a positive integer depth Example 1: Input:
A binary tree as following:
4
/ \
2 6
/ \ /
3 1 5
v = 1
d = 2
Output:
4
/ \
1 1
/ \
2 6
/ \ /
3 1 5
Example 2: Input: A binary tree as following: 4 / 2 / \ 3 1 v = 1 d = 3 Output: 4 / 2 / \ 1 1 / \ 3 1 Note:
给定一个二叉树,根节点为第1层,深度为 1。在其第 添加规则:给定一个深度值 将 如果 示例 1: 输入: 二叉树如下所示: 4 / \ 2 6 / \ / 3 1 5 v = 1 d = 2 输出: 4 / \ 1 1 / \ 2 6 / \ / 3 1 5 示例 2: 输入: 二叉树如下所示: 4 / 2 / \ 3 1 v = 1 d = 3 输出: 4 / 2 / \ 1 1 / \ 3 1 注意:
Runtime: 36 ms
Memory Usage: 20.6 MB
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 addOneRow(_ root: TreeNode?, _ v: Int, _ d: Int) -> TreeNode? { 16 if d == 0 || d == 1 17 { 18 var newRoot:TreeNode? = TreeNode(v) 19 if d == 0 20 { 21 newRoot!.right = root 22 } 23 else 24 { 25 newRoot!.left = root 26 } 27 return newRoot 28 } 29 if root != nil && d > 1 30 { 31 root!.left = addOneRow(root!.left, v, d > 2 ? d - 1 : 1) 32 root!.right = addOneRow(root!.right, v, d > 2 ? d - 1 : 0) 33 } 34 return root 35 } 36 } 44ms 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 addOneRow(_ root: TreeNode?, _ v: Int, _ d: Int) -> TreeNode? { 16 if d == 1 { 17 let newRoot = TreeNode(v) 18 newRoot.left = root 19 return newRoot 20 } 21 helper(root, v, 1, d) 22 return root 23 } 24 25 private func helper(_ root: TreeNode?, _ v: Int, _ d: Int, _ targetD: Int) { 26 guard let root = root else { return } 27 if d == targetD - 1 { 28 let curLeft = root.left 29 let curRight = root.right 30 root.left = TreeNode(v) 31 root.right = TreeNode(v) 32 root.left?.left = curLeft 33 root.right?.right = curRight 34 return 35 } 36 helper(root.left, v, d + 1, targetD) 37 helper(root.right, v, d + 1, targetD) 38 } 39 } 48ms 1 class Solution { 2 func addOneRow(_ root: TreeNode?, _ v: Int, _ d: Int) -> TreeNode? { 3 if root == nil { 4 return nil 5 } 6 7 var root = root 8 if d == 1 { 9 let newNode = TreeNode(v) 10 newNode.left = root 11 root = newNode 12 13 return root 14 } 15 16 var nodes = [(root, 1)] 17 while let (node, level) = nodes.popLast(), level < d { 18 if let left = node?.left { 19 nodes.insert((left, level + 1), at: 0) 20 } 21 if let right = node?.right { 22 nodes.insert((right, level + 1), at: 0) 23 } 24 25 if level == d - 1 { 26 let leftNode = TreeNode(v) 27 leftNode.left = node?.left 28 node?.left = leftNode 29 30 let rightNode = TreeNode(v) 31 rightNode.right = node?.right 32 node?.right = rightNode 33 } 34 35 if level > d { 36 break 37 } 38 } 39 40 return root 41 } 42 } 60ms 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 Solution { 16 func addOneRow(_ root: TreeNode?, _ v: Int, _ d: Int) -> TreeNode? { 17 if d == 1 { 18 var node = TreeNode(v) 19 node.left = root 20 return node 21 } 22 23 var stack = [TreeNode?]() 24 var tempArr = [TreeNode?]() 25 var currLevel = 1 26 stack.append(root) 27 while currLevel != d - 1 { 28 //print("currLevel: \(currLevel) ==============") 29 while !stack.isEmpty { 30 guard let node = stack.removeLast() else { 31 continue 32 } 33 //print("node: \(node.val)") 34 if let lNode = node.left { 35 tempArr.append(lNode) 36 } 37 if let rNode = node.right { 38 tempArr.append(rNode) 39 } 40 } 41 stack.append(contentsOf: tempArr) 42 tempArr.removeAll() 43 currLevel += 1 44 } 45 46 while !stack.isEmpty { 47 guard let node = stack.removeLast() else { 48 continue 49 } 50 //print("===== node: \(node.val)") 51 if let lNode = node.left { 52 var vNode = TreeNode(v) 53 vNode.left = lNode 54 node.left = vNode 55 } else { 56 var vNode = TreeNode(v) 57 node.left = vNode 58 } 59 if let rNode = node.right { 60 var vNode = TreeNode(v) 61 vNode.right = rNode 62 node.right = vNode 63 } else { 64 var vNode = TreeNode(v) 65 node.right = vNode 66 } 67 } 68 return root 69 } 70 } 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 class Solution { 15 func addOneRow(_ root: TreeNode?, _ v: Int, _ d: Int) -> TreeNode? { 16 if d == 1 { 17 let node = TreeNode(v) 18 node.left = root 19 return node 20 } 21 22 23 if root == nil { 24 return nil 25 } 26 27 let ans = root 28 traverseAddOneRow(root!, v, d-1) 29 return ans 30 } 31 32 func traverseAddOneRow(_ root: TreeNode?, _ v: Int, _ d: Int) { 33 if root == nil { 34 return 35 } 36 37 38 if d < 1 { 39 return 40 } 41 42 if d == 1 { 43 44 let lastl = root!.left 45 let l = TreeNode(v) 46 root!.left = l 47 l.left = lastl 48 49 let lastr = root!.right 50 let r = TreeNode(v) 51 root!.right = r 52 r.right = lastr 53 54 return 55 } 56 traverseAddOneRow(root!.left, v, d-1) 57 traverseAddOneRow(root!.right, v, d-1) 58 } 59 60 }
|
请发表评论