leetcode-cn Daily Challenge on September 16th, 2020.
Difficulty : Easy
Related Topics : Tree
Invert a binary tree.
Input:
4 / \ 2 7 / \ / \ 1 3 6 9
Output:
4 / \ 7 2 / \ / \ 9 6 3 1
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 f*** off.
- mine
- Java
Runtime: 0 ms, faster than 100.00%, Memory Usage: 37.2 MB, less than 22.55% of Java online submissions
//O(D)time O(1)space //D is root deepth public TreeNode invertTree(TreeNode root) { if(root == null){ return null; } TreeNode t = root.left; root.left = invertTree(root.right); root.right = invertTree(t); return root; }
- Java