• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[Swift]LeetCode623.在二叉树中增加一行|AddOneRowtoTree

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10472716.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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.

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.

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. The given d is in range [1, maximum depth of the given tree + 1].
  2. The given binary tree has at least one tree node.

给定一个二叉树,根节点为第1层,深度为 1。在其第 d 层追加一行值为 v 的节点。

添加规则:给定一个深度值 d (正整数),针对深度为 d-1 层的每一非空节点 N,为 N 创建两个值为 v 的左子树和右子树。

将 N 原先的左子树,连接为新节点 v 的左子树;将 N 原先的右子树,连接为新节点 v 的右子树。

如果 d 的值为 1,深度 d - 1 不存在,则创建一个新的根节点 v,原先的整棵树将作为 v 的左子树。

示例 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. 输入的深度值 d 的范围是:[1,二叉树最大深度 + 1]。
  2. 输入的二叉树至少有一个节点。

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 }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift/Objective-C-Swift与Objective-C混用教程发布时间:2022-07-13
下一篇:
使用 Swift 创建简单的二维码扫描应用发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap