力扣【226】翻转二叉树

题目:

翻转一棵二叉树。

示例:

输入:

     4
   /   \
  2     7
 / \   / \
1   3 6   9
输出:

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

题解:

class Solution {
    public TreeNode invertTree(TreeNode root) {
        //边界条件
        if (root == null) {
            return null;
        }
        TreeNode tem = root.left;
        root.left = root.right;
        root.right = tem;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}

猜你喜欢

转载自blog.csdn.net/qq1922631820/article/details/111088997