Recursive Solution in Golang


  • 0
    J
    type TreeNode struct {
    	Val int
    	LeftTreeNode
    	RightTreeNode
    }
    
    func flatten(root *TreeNode) {
    	if root == nil || (root.Left == nil && root.Right == nil) {
    		return
    	}
    	flatten(root.Left)
    	flatten(root.Right)
    	currRight := root.Right
    	root.Right = root.Left
    	root.Left = nil
    	for root.Right != nil {
    		root = root.Right
    	}
    	root.Right = currRight
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.