力扣 | 226. 翻转二叉树

力扣 | 226. 翻转二叉树_第1张图片

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

你可能感兴趣的:(算法学习,leetcode,算法)