zoukankan      html  css  js  c++  java
  • 426. Convert Binary Search Tree to Sorted Doubly Linked List

    /**
     * 426. Convert Binary Search Tree to Sorted Doubly Linked List
     * https://www.lintcode.com/problem/convert-binary-search-tree-to-sorted-doubly-linked-list/description
     * */
    class Solution {
        //save last access node
        var prev: TreeNode? = null
    
        fun treeToDoublyList(root: TreeNode?): TreeNode? {
            if (root == null) {
                return null
            }
            //the leftmost node
            val dummy = TreeNode(-1)
            //these two node point to the same
            prev = dummy
            inorder(root)
            //link each other
            val head = dummy.right
            prev?.right = head
            head?.left = prev
            return head
        }
    
        //changing prev
        private fun inorder(node: TreeNode?) {
            if (node == null) {
                return
            }
            inorder(node.left)
            //link each other
            prev?.right = node
            node.left = prev
            prev = node
            inorder(node.right)
        }
    }
  • 相关阅读:
    VBA的几个小Demo_2
    VBA的几个小Demo
    Django部署在阿里云服务器上
    python面试题及解析
    Django知识扩展
    Django文件下载2
    Django文件下载
    Django文件上传
    My_First_Web
    10个jQuery小技巧
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/12558981.html
Copyright © 2011-2022 走看看