目录
- [1- 思路](#1- 思路)
-
- [Queue 队列实现层序遍历 + 交换左右](#Queue 队列实现层序遍历 + 交换左右)
- [2- 实现](#2- 实现)
-
- [⭐226. 翻转二叉树------题解思路](#⭐226. 翻转二叉树——题解思路)
- [3- ACM 实现](#3- ACM 实现)
- 原题连接:226. 翻转二叉树
1- 思路
Queue 队列实现层序遍历 + 交换左右
- 1- 借助
Queue
实现层序遍历 - 2- 实现左右交换方式
2- 实现
⭐226. 翻转二叉树------题解思路
java
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
// 借助队列实现层序交换
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
while(len>0){
TreeNode nowNode = queue.poll();
swapNode(nowNode);
if(nowNode.left!=null) queue.add(nowNode.left);
if(nowNode.right!=null) queue.add(nowNode.right);
len--;
}
}
return root;
}
public void swapNode(TreeNode node){
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
}
}
3- ACM 实现
java
public class invertTree {
public static 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;
}
}
public static TreeNode build(String str){
if(str == null || str.length()==0){
return null;
}
String input = str.replace("[","");
input = input.replace("]","");
String[] parts = input.split(",");
Integer[] nums = new Integer[parts.length];
for(int i = 0 ; i < parts.length;i++){
if(!parts[i].equals("null")){
nums[i] = Integer.parseInt(parts[i]);
}else{
nums[i] = null;
}
}
Queue<TreeNode> queue = new LinkedList<>();
TreeNode root = new TreeNode(nums[0]);
queue.offer(root);
int index = 1;
while(!queue.isEmpty()&& index<parts.length){
TreeNode node = queue.poll();
if(index<nums.length && nums[index]!=null){
node.left = new TreeNode(nums[index]);
queue.offer(node.left);
}
index++;
if(index<nums.length && nums[index]!=null){
node.right = new TreeNode(nums[index]);
queue.offer(node.right);
}
index++;
}
return root;
}
public static TreeNode invertTree(TreeNode root) {
if(root==null){
return root;
}
// 借助队列实现层序交换
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
while(len>0){
TreeNode nowNode = queue.poll();
swapNode(nowNode);
if(nowNode.left!=null) queue.add(nowNode.left);
if(nowNode.right!=null) queue.add(nowNode.right);
len--;
}
}
return root;
}
public static void swapNode(TreeNode node){
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
}
static List<Integer> levelRes = new ArrayList<>();
public static TreeNode levelT(TreeNode root) {
if(root==null){
return root;
}
// 借助队列实现层序交换
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
int len = queue.size();
while(len>0){
TreeNode nowNode = queue.poll();
levelRes.add(nowNode.val);
if(nowNode.left!=null) queue.add(nowNode.left);
if(nowNode.right!=null) queue.add(nowNode.right);
len--;
}
}
return root;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
TreeNode root = build(input);
root = invertTree(root);
levelT(root);
System.out.println("结果是"+levelRes.toString());
}
}