Leetcode Note: Go - Invert Binary Tree

Invert Binary Tree - LeetCode
https://leetcode.com/problems/invert-binary-tree/

  • Go 言語で取り組んだメモ

所感

  • 再帰で実装だろうか

回答

[Go] 4 lines solution - Clean solution - Recursive (0ms 100%) - LeetCode Discuss
https://leetcode.com/problems/invert-binary-tree/discuss/1695693/Go-4-lines-solution-Clean-solution-Recursive-(0ms-100)

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func invertTree(root *TreeNode) *TreeNode {
    if root != nil {
        root.Left, root.Right = invertTree(root.Right), invertTree(root.Left)
    }
    return root
}
  • なんてシンプルなソリューションなんだ
  • 複数の変数を同時に代入する書き方は思ってるよりもパワフルな機能