Skip to content

Latest commit

 

History

History
61 lines (53 loc) · 1.11 KB

226. Invert Binary Tree.md

File metadata and controls

61 lines (53 loc) · 1.11 KB

leetcode-cn Daily Challenge on September 16th, 2020.


Difficulty : Easy

Related Topics : Tree


Invert a binary tree.

Example:

Input:

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

Output:

     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 f*** off.


Solution

  • 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;
        }