★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-rabgexca-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given the root of a binary tree, then value v
and depth d
, you need to add a row of nodes with value v
at the given depth d
. The root node is at depth 1.node
The adding rule is: given a positive integer depth d
, for each NOT null tree nodes N
in depth d-1
, create two tree nodes with value v
as N's
left subtree root and right subtree root. And N's
original left subtree should be the left subtree of the new left subtree root, its original right subtree should be the right subtree of the new right subtree root. If depth d
is 1 that means there is no depth d-1 at all, then create a tree node with value v as the new root of the whole original tree, and the original tree is the new root's left subtree.git
Example 1:github
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:app
給定一個二叉樹,根節點爲第1層,深度爲 1。在其第 d
層追加一行值爲 v
的節點。spa
添加規則:給定一個深度值 d
(正整數),針對深度爲 d-1
層的每一非空節點 N
,爲 N
建立兩個值爲 v
的左子樹和右子樹。code
將 N
原先的左子樹,鏈接爲新節點 v
的左子樹;將 N
原先的右子樹,鏈接爲新節點 v
的右子樹。htm
若是 d
的值爲 1,深度 d - 1 不存在,則建立一個新的根節點 v
,原先的整棵樹將做爲 v
的左子樹。blog
示例 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
注意:
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 }