226.Invert Binary Tree(Easy)

Invert a binary tree.
反转二叉树

  基本上二叉树的玩意儿用递归都能做

For example


to


My Solution

(Java) Version 1 Time: 1ms:

  简单地递归然后调换左右子树

/**
 * 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) {
            return null;
        }
        root.left = invertTree(root.left);
        root.right = invertTree(root.right);

        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        return root;
    }
}

(Java) Version 2 Time: 0ms (By anjani_sureshbhat):

  有递归调用自然就有非递归调用,这就是一个迭代的版本,然而我的迭代写得并不好,还要多看,当然还有一个很重要的就是迭代比递归快

/**
 * 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) {
            return null;
        }
        Queue q = new LinkedList();
        q.offer(root);
        while (!q.isEmpty()) {
            TreeNode polled = q.poll();
            if (polled.left != null || polled.right != null) {
                TreeNode temp = polled.left;
                polled.left = polled.right;
                polled.right = temp;
                if (polled.left != null) q.offer(polled.left);
                if (polled.right != null) q.offer(polled.right);
            }
        }
        return root;
    }
}

你可能感兴趣的:(226.Invert Binary Tree(Easy))