LintCode - 翻转二叉树(普通)

版权声明:本文为博主原创文章,未经博主允许不得转载。

难度:容易
要求:

翻转一棵二叉树
样例

  1         1
 / \       / \
2   3  => 3   2
   /       \
  4         4

思路

/**
     * @param root: a TreeNode, the root of the binary tree
     * @return: nothing
     */
    public void invertBinaryTree(TreeNode root) {
        if(root == null){
            return;
        }
        
        TreeNode node = root.left;
        root.left = root.right;
        root.right = node;
        
        invertBinaryTree(root.left);
        invertBinaryTree(root.right);
    }

你可能感兴趣的:(LintCode - 翻转二叉树(普通))