underwindfall / Algorithme

练习总结算法的地方
https://qifanyang.com/resume
1 stars 0 forks source link

LCOF27 #368

Closed underwindfall closed 2 years ago

underwindfall commented 2 years ago
// time O(n)
    // space O(n)
    class DFS {
        public TreeNode mirrorTree(TreeNode root) {
            if (root == null)
                return root;
            if (root.left == null && root.right == null) {
                return root;
            }
            TreeNode newLeft = mirrorTree(root.left);
            TreeNode newRight = mirrorTree(root.right);
            root.left = newRight;
            root.right = newLeft;
            return root;
        }
    }

    // time O(n)
    // space O(n)
    class BFS {
        public TreeNode mirrorTree(TreeNode root) {
            Queue<TreeNode> q = new LinkedList<>();
            q.offer(root);
            while (!q.isEmpty()) {
                TreeNode tmp = q.poll();
                if (tmp == null)
                    continue;
                TreeNode left = tmp.left;
                tmp.left = tmp.right;
                tmp.right = left;
                if (tmp.left != null) {
                    q.offer(tmp.left);
                }
                if (tmp.right != null) {
                    q.offer(tmp.right);
                }
            }
            return root;
        }
    }