leetcode226. 翻转二叉树

1.题目描述:翻转一棵二叉树。

leetcode226. 翻转二叉树_第1张图片leetcode226. 翻转二叉树_第2张图片

2.自底向上递归:

递归三步骤,①递归终止条件:root = null,返回null。②当前递归需要做的:交换当前节点的左右子树,当前节点的左右子树为已经交换完的子树,也就是通过递归得到的子树。③返回值:返回子树交换后的当前节点。考虑到遍历所有节点及最坏成链表的情况,时间空间复杂度均为O(n),代码如下:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return null;
        TreeNode temp = root.left;//需要定义临时变量保存左子树
        root.left = invertTree(root.right);
        root.right = invertTree(temp);
        return root;
    }
}

3.自顶向下递归:

递归三步骤,①递归终止条件:root = null,返回null。②当前递归需要做的:直接交换当前节点的左右子树,然后再通过递归交换左节点的两颗子树和右节点的两颗子树。③返回值:返回子树交换后的当前节点。考虑到遍历所有节点及最坏成链表的情况,时间空间复杂度均为O(n),代码如下:

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return null;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
}

4.广度优先搜索迭代:

使用队列对每一层的节点的左右子树进行反转,思路跟leetcode104. 二叉树的最大深度第三种方法类似,遍历所有节点及队列最大接近n,时间空间复杂度均为O(n),这里直接上代码:

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return null;
        Queue queue = new LinkedList<>();
        queue.offer(root);
        TreeNode curNode = null;
        TreeNode temp = null;
        while(!queue.isEmpty()){
            curNode = queue.poll();
            temp = curNode.left;
            curNode.left = curNode.right;
            curNode.right = temp;
            if(curNode.left != null) queue.offer(curNode.left);
            if(curNode.right != null) queue.offer(curNode.right);
        }
        return root;
    }
}

5.深度优先搜索迭代:

使用栈先进后出的特性实现深度优先子树的反转,遍历所有节点及队列最大接近n,时间空间复杂度均为O(n),这里直接上代码:

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null) return null;
        Stack stack = new Stack<>();
        stack.push(root);
        TreeNode curNode = null;
        TreeNode temp = null;
        int size = 0;
        while(!stack.isEmpty()){
            size = stack.size();
            while(size > 0){
                curNode = stack.pop();
                size--;
                temp = curNode.left;
                curNode.left = curNode.right;
                curNode.right = temp;
                if(curNode.left != null) stack.push(curNode.left);
                if(curNode.right != null) stack.push(curNode.right);
            }
        }
        return root;
    }
}
​

6.二刷递归:

class Solution {
    public TreeNode invertTree(TreeNode root) {
        helper(root);
        return root;
    }
    public void helper(TreeNode root) {
        if (root == null) return;
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        helper(root.left);
        helper(root.right);
    }    
}

层序遍历:遍历每个节点然后交换其左右子树即可。

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) return root;
        Queue queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size > 0) {
                TreeNode cur = queue.poll();
                TreeNode temp = cur.left;
                cur.left = cur.right;
                cur.right = temp;
                if (cur.left != null) queue.offer(cur.left);
                if (cur.right != null) queue.offer(cur.right);
                size--;
            }
        }
        return root;
    }
}

你可能感兴趣的:(树,算法,数据结构,树结构,leetcode,java)