226.Invert Binary Tree

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
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;
        
    }
}

 

相关文章: