Invert a binary tree.

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

to

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

Trivia:
This problem was inspired by this original tweet by Max Howell:

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.
public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)  return null;
        TreeNode right=root.right;
        TreeNode left =root.left;
        root.right=invertTree(left);
        root.left=invertTree(right);
        return root;
    }
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2021-07-25
  • 2021-09-01
猜你喜欢
  • 2022-12-23
  • 2021-08-16
  • 2021-06-16
  • 2022-02-14
  • 2021-12-20
  • 2021-08-28
  • 2022-12-23
相关资源
相似解决方案