美文网首页
剑指 Offer II 052. 展平二叉搜索树

剑指 Offer II 052. 展平二叉搜索树

作者: 邦_ | 来源:发表于2022-06-30 11:23 被阅读0次
    
    func increasingBST(_ root: TreeNode?) -> TreeNode? {
        
            var array = Array<Int>()
            dealTree(root, &array)
            var node : TreeNode?
            var tempNode : TreeNode?
    
            for i in 0..<array.count  {
                
                if node == nil {
                    node = TreeNode(array[i])
                    tempNode = node
                }else {
                    
                    let node1 = TreeNode(array[i])
                    node?.right = node1
                    node = node1
                }
                
            }
            
            return tempNode
            
        }
        
        func dealTree(_ root:TreeNode? ,_ array:inout Array<Int>) {
            
            if root == nil {
                return
            }
            
            dealTree(root?.left,&array)
            array.append((root?.val)!)
            dealTree(root?.right,&array)
            
        }
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:剑指 Offer II 052. 展平二叉搜索树

          本文链接:https://www.haomeiwen.com/subject/esttyrtx.html