LeetCode - 226. Invert Binary Tree

链接

226. Invert Binary Tree

题意

翻转二叉树。

思路

利用递归交换结点即可。

代码

Java:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root != null) {
            TreeNode t = root.left;
            root.left = root.right;
            root.right = t;
            invertTree(root.left);
            invertTree(root.right);
        }
        return root;

    }
}

效率

Your runtime beats 30.77% of java submissions.

posted @ 2017-04-09 00:49  zyoung  阅读(126)  评论(0编辑  收藏  举报