LeetCode 226. Invert Binary Tree 翻转二叉树(Java)

题目:

Invert a binary tree.
LeetCode 226. Invert Binary Tree 翻转二叉树(Java)_第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.

解答:

采用递归的思路,比较简单不赘述

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        TreeNode temp=root.left;
        root.left=invertTree(root.right);
        root.right=invertTree(temp);
        return root;
    }     
}

你可能感兴趣的:(LeetCode)