博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode538. 把二叉搜索树转换为累加树 | Convert BST to Greater Tree
阅读量:5063 次
发布时间:2019-06-12

本文共 4982 字,大约阅读时间需要 16 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

Example:

Input: The root of a Binary Search Tree like this:              5            /   \           2     13Output: The root of a Greater Tree like this:             18            /   \          20     13

给定一个二叉搜索树(Binary Search Tree),把它转换成为累加树(Greater Tree),使得每个节点的值是原来的节点值加上所有大于它的节点值之和。

例如:

输入: 二叉搜索树:              5            /   \           2     13输出: 转换为累加树:             18            /   \          20     13

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 = nil10  *         self.right = nil11  *     }12  * }13  */14 class Solution {15     private var sum = 016     func convertBST(_ root: TreeNode?) -> TreeNode? {17        // in-order dfs18         if root != nil {19         convertBST(root?.right)20         sum += root!.val21         root!.val = sum22         convertBST(root?.left)23         }24         return root25     }26 }

84ms

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 = nil10  *         self.right = nil11  *     }12  * }13  */14 class Solution {15     16     func convertBST(_ root: TreeNode?) -> TreeNode? {17         guard let root = root else { return nil }18         19         traverse(root)20         21         return root22     }23     24     var sum = 025     26     func traverse(_ root: TreeNode?) {27         guard let root = root else { return }28         29         traverse(root.right)30         sum += root.val31         root.val = sum32         traverse(root.left)33     }34 }

92ms

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 = nil10  *         self.right = nil11  *     }12  * }13  */14 class Solution {15     16     var sum = 017     18     func convertBST(_ root: TreeNode?) -> TreeNode? {19         if root == nil {20             return root  21         } 22         convertBST(root!.right)23         root!.val = root!.val + sum24         sum = root!.val25         convertBST(root!.left)26         27         return root        28     }  29 }

100ms

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 = nil10  *         self.right = nil11  *     }12  * }13  */14 class Solution {15      func convertBST(_ root: TreeNode?) -> TreeNode? {16         var sum = 017          var stack = Stack()18          19         var node = root20          21          while !stack.isEmpty || node != nil {22              while node != nil {23                  stack.push(node!)24                  node = node?.right25              }26              27              node = stack.pop()28              sum += node!.val29              node!.val = sum30              31              node = node?.left32          }33          34          return root35     }36 }37 38 class Stack {39     private var array = [TreeNode]()40     41     var isEmpty: Bool { return array.isEmpty }42     43     func push(_ node: TreeNode) {44         array.append(node)45     }46     47     func pop() -> TreeNode {48         return array.popLast()!49     }50 }

120ms

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 = nil10  *         self.right = nil11  *     }12  * }13  */14 class Solution {15     func convertBST(_ root: TreeNode?) -> TreeNode? {16         _ = convertBST(root, 0)17         return root18     }19 20     func convertBST(_ root: TreeNode?, _ num: Int) -> Int {21         guard let root = root else {22             return num23         }24         25         var num = convertBST(root.right, num)26         root.val += num27         num = root.val28         num = convertBST(root.left, num)29         return num30     }31 }
 

转载于:https://www.cnblogs.com/strengthen/p/9817299.html

你可能感兴趣的文章
鼠标移到行上显示,移出消失效果
查看>>
做过的项目
查看>>
ubuntu14.04 +nginx+php5-fpm
查看>>
(转)最大类间方差法(Otsu)
查看>>
常用jar包下载地址汇总
查看>>
Java发送邮箱
查看>>
HDU1024 - Max Sum Plus Plus(DP+降维优化)
查看>>
English trip M1 - AC11 I Dreamed a Dream? 我做了一个梦 Teacher:Lamb
查看>>
代码大全阅读笔记03
查看>>
MySQL--Profiling和Trace使用
查看>>
Windows系统下GitBash显示的中文乱码解决方案
查看>>
匿名内部类实现线程的两种方式
查看>>
20141229 mysql基本操作二
查看>>
vc6 bug真多 写c++别用vc6
查看>>
MySQL:日期类型
查看>>
php中利用reset,current,next和each,list来遍历数组
查看>>
用Sql语句查询表结构
查看>>
某年某月某天
查看>>
三层 之 充值
查看>>
vue2.0实践的一些细节
查看>>