题解 | #翻转牛群结构#
翻转牛群结构
https://www.nowcoder.com/practice/f17e1aa5329f492f9107cbe303222ad6
知识点:二叉树,递归。
分析:针对每个节点,翻转其左右子节点,再递归其左右子节点,返回当前节点,截至条件可设置为当前节点为null。
import java.util.*;
public 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;
}
}
