题目来源:

https://leetcode-cn.com/problems/invert-binary-tree/

题目描述:

LeetCode226.翻转二叉树

 解题思路:先交换左右子树,然后分别递归左子树和右子树。

代码如下:

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

 

相关文章:

  • 2022-12-23
  • 2021-10-15
  • 2021-08-25
  • 2021-06-05
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
  • 2021-10-10
猜你喜欢
  • 2021-07-28
  • 2022-12-23
  • 2021-10-20
  • 2021-12-03
  • 2021-09-01
  • 2021-06-14
  • 2021-06-30
相关资源
相似解决方案