zoukankan      html  css  js  c++  java
  • Go语言实现:【剑指offer】二叉搜索树与双向链表

    该题目来源于牛客网《剑指offer》专题。

    输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

    Go语言实现:

    type TreeNode struct {
    	Val   int
    	Left  *TreeNode
    	Right *TreeNode
    }
    
    var leftLast *TreeNode
    
    //递归
    func convert(root *TreeNode) *TreeNode{
       if root == nil {
          return nil
       }
       if root.Left == nil && root.Right == nil {
          leftLast = root
          return root
       }
       //左子树
       left := convert(root.Left)
       if left != nil {
          leftLast.Right = root
          root.Left = leftLast
       }
       leftLast = root
       //右子树
       right := convert(root.Right)
       if right != nil {
          right.Left = root
          root.Right = right
       }
       //?
       if left != nil {
          return left
       } else {
          return root
       }
    }
    
  • 相关阅读:
    闭包
    this
    函数声明,表达式,构造函数
    算法学习_栈
    LeetCode刷题_140
    2020/3/20 刷题
    2020/3/19 刷题
    2020/3/13_C++实验课
    刷题(主要是DFS) 2020年3月12日
    DFS的一些题2020/3/11
  • 原文地址:https://www.cnblogs.com/dubinyang/p/12099418.html
Copyright © 2011-2022 走看看