LeetCode 226. Invert Binary Tree 题解 —— Java

题目链接:https://leetcode.com/problems/invert-binary-tree/#/description

题目要求:反转二叉树——左子树换成右子树,右子树换成左子树

思路:首先将根节点的左右子树交换,然后递归的将左右子树再进行反转。


Java代码:

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


你可能感兴趣的:(LeetCode题解)