Closed underwindfall closed 2 years ago
https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/
思路
[x] 刷15mins
[x] 刷5mins
// 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; } }
https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/
思路
[x] 刷15mins
[x] 刷5mins