[Swift]LeetCode998. 最大二叉樹 II | Maximum Binary Tree II

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-mvspnmqj-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree.node

Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with the following Construct(A) routine:git

  • If A is empty, return null.
  • Otherwise, let A[i] be the largest element of A.  Create a root node with value A[i].
  • The left child of root will be Construct([A[0], A[1], ..., A[i-1]])
  • The right child of root will be Construct([A[i+1], A[i+2], ..., A[A.length - 1]])
  • Return root.

Note that we were not given A directly, only a root node root = Construct(A).github

Suppose B is a copy of A with the value val appended to it.  It is guaranteed that B has unique values.微信

Return Construct(B)app

Example 1:spa

Input: root = [4,1,3,null,null,2], val = 5 Output: [5,4,null,1,3,null,null,2] Explanation: A = [1,4,2,3], B = [1,4,2,3,5] 

Example 2:
code

Input: root = [5,2,4,null,1], val = 3 Output: [5,2,4,null,1,null,3] Explanation: A = [2,1,5,4], B = [2,1,5,4,3] 

Example 3:
htm

Input: root = [5,2,3,null,1], val = 4 Output: [5,2,4,null,1,3] Explanation: A = [2,1,5,3], B = [2,1,5,3,4] 

Note:blog

  1. 1 <= B.length <= 100

最大樹定義:一個樹,其中每一個節點的值都大於其子樹中的任何其餘值。

給出最大樹的根節點 root

就像以前的問題那樣,給定的樹是從表 Aroot = Construct(A))遞歸地使用下述 Construct(A) 例程構造的:

  • 若是 A 爲空,返回 null
  • 不然,令 A[i] 做爲 A 的最大元素。建立一個值爲 A[i] 的根節點 root
  • root 的左子樹將被構建爲 Construct([A[0], A[1], ..., A[i-1]])
  • root 的右子樹將被構建爲 Construct([A[i+1], A[i+2], ..., A[A.length - 1]])
  • 返回 root

請注意,咱們沒有直接給定 A,只有一個根節點 root = Construct(A).

假設 B 是 A 的副本,並附加值 val。保證 B 中的值是不一樣的。

返回 Construct(B)。 

示例 1:

輸入:root = [4,1,3,null,null,2], val = 5
輸出:[5,4,null,1,3,null,null,2]
解釋:A = [1,4,2,3], B = [1,4,2,3,5]

示例 2:

輸入:root = [5,2,4,null,1], val = 3
輸出:[5,2,4,null,1,null,3]
解釋:A = [2,1,5,4], B = [2,1,5,4,3]

示例 3:

輸入:root = [5,2,3,null,1], val = 4
輸出:[5,2,4,null,1,3]
解釋:A = [2,1,5,3], B = [2,1,5,3,4] 

提示:

  1. 1 <= B.length <= 100

Runtime: 16 ms
Memory Usage: 18.7 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 insertIntoMaxTree(_ root: TreeNode?, _ val: Int) -> TreeNode? {
16         var root = root
17         var X:TreeNode? = TreeNode(val)
18         if root == nil
19         {
20             return X
21         }
22         if root!.val < val
23         {
24             X!.left = root
25             return X
26         }        
27         dfs(&root, X)
28         return root
29     }
30     
31     func dfs(_ root: inout TreeNode?, _ X: TreeNode?)
32     {
33         if root!.right == nil || root!.right!.val < X!.val
34         {
35             var Y:TreeNode? = root!.right
36             root?.right = X
37             X?.left = Y
38             return
39         }
40         dfs(&root!.right, X)
41     }
42 }

16ms

 

 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 insertIntoMaxTree(_ root: TreeNode?, _ val: Int) -> TreeNode? {
16         return constructMax(root, val)
17     }
18     
19     func constructMax(_ root: TreeNode?, _ val: Int) -> TreeNode? {
20         guard let current = root else { return TreeNode(val) }
21         if val > current.val {
22             let newNode = TreeNode(val)
23             newNode.left = current
24             return newNode
25         }
26         current.right = constructMax(current.right, val)
27         return current
28     }
29 }
相關文章
相關標籤/搜索