Fork me on GitHub

Lintcode 175. 翻转二叉树

 

--------------------

 

递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o)

 

AC代码:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: a TreeNode, the root of the binary tree
     * @return: nothing
     */
    public void invertBinaryTree(TreeNode root) {
        if(root==null) return ;
        TreeNode t=root.left;
        root.left=root.right;
        root.right=t;
        invertBinaryTree(root.left);
        invertBinaryTree(root.right);
    }
}

 

 

题目来源: http://www.lintcode.com/zh-cn/problem/invert-binary-tree/

 

posted @ 2017-01-01 23:51  CC11001100  阅读(212)  评论(0编辑  收藏  举报