leetcode: 226. Invert Binary Tree

Love The Way You Lie 2022-07-26 06:20 232阅读 0赞

leetcode: 226. Invert Binary Tree

Invert a binary tree.

  1. 4
  2. / \ 2 7
  3. / \ / \ 1 3 6 9

to

  1. 4
  2. / \ 7 2
  3. / \ / \ 9 6 3 1

没啥好说的

  1. /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */
  2. func invertTree(root *TreeNode) *TreeNode {
  3. if root == nil {
  4. return nil
  5. }
  6. root.Left, root.Right = root.Right, root.Left
  7. if root.Left != nil {
  8. invertTree(root.Left)
  9. }
  10. if root.Right != nil {
  11. invertTree(root.Right)
  12. }
  13. return root
  14. }

那就黑一把谷歌吧233

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.

虽然是事实,但权当一乐

发表评论

表情:
评论列表 (有 0 条评论,232人围观)

还没有评论,来说两句吧...

相关阅读