### 6.6 226. Invert Binary Tree
Given the root of a binary tree, invert the tree, and return its root.
交换的是指针,不是数值!
前序/后序遍历最简洁,中序会比较绕。其中后序最简洁,因为省去了判断节点左和右是否为空的冗余代码。总之都是可以实现的。
>[!tip] 继续递归三部曲:
1.确认递归函数的参数和返回值:大多数二叉树算法的参数都只有节点和返回值
2.确定终止条件:遇到空节点的时候返回
3.确定单层递归的逻辑:
```java
public class invertBT {
public TreeNode invertTree(TreeNode root) {
if(root == null) return root;
invertTree(root.left);
invertTree(root.right);
swapChilderen(root);
return root;
}
public void swapChilderen(TreeNode node){
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
}
}
class invertBTTest {
public static void main(String[] args) {
invertBT ibt = new invertBT();
TreeBuilder tb = new TreeBuilder();
TreeNode root = tb.buildTree();
root = ibt.invertTree(root);
//I use lever order tarversal here, because it's more easy to imagine the tree's picture.
invertBTTest ibtt = new invertBTTest();
List<List<Integer>> list = ibtt.levelOrderTraversal(root);
for(List<Integer> levels :list){
System.out.print("[");
for(int nums : levels){
System.out.print(nums+" ");
}
System.out.println("]");
}
}
public List<List<Integer>> levelOrderTraversal(TreeNode node){
List<List<Integer>> result = new LinkedList<>();
if(node == null) return result;
Deque<TreeNode> deque = new LinkedList<>();
deque.offerLast(node);
while(!deque.isEmpty()){
int size = deque.size();
List<Integer> nums = new LinkedList<>();
while(size > 0){
TreeNode tmp = deque.pollFirst();
nums.add(tmp.val);
if(tmp.left != null) deque.offerLast(tmp.left);
if(tmp.right != null) deque.offerLast(tmp.right);
size--;
}
result.add(nums);
}
return result;
}
}
```
### 6.7 101. Symmetric Tree
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
看它能否翻转。
本题只能使用后续遍历。因为需要不断地返回左右的信息给中,所以只有左右中的顺序可以。
总结:需要收集孩子信息返回给上一级的使用后序。
```java
public class symmetricTree {
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return compare(root.left,root.right);
}
public boolean compare(TreeNode left, TreeNode right){
//左右都为空/一个为空/都不为空但是值不相等/都不为空且值相等
if(left == null && right == null) return true;
if(left == null && right != null) return false;
if(left != null && right == null) return false;
if(left.val != right.val) return false;
boolean inside = compare(left.right,right.left);
boolean outside = compare(left.left,right.right);
return inside && outside;
}
}
class symmetricTreeTest {
public static void main(String[] args) {
symmetricTree st = new symmetricTree();
TreeBuilder tb = new TreeBuilder();
TreeNode root = tb.buildTree();
boolean isSymmetric = st.isSymmetric(root);
System.out.println(isSymmetric);
}
}
```
### 6.8 104. Maximum Depth of Binary Tree (use recursion traversal)
Given the root of a binary tree, return its maximum depth.
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
>[!Tip] 高度 & 深度
>深度:二叉树中任意一个节点到根节点的举例。用后序遍历,左右中。因为是由末端节点每层返回+1给父节点。
>
高度:二叉树中任意一个节点到叶子节点的举例。用前序遍历,中左右。
此题根节点的高度就是二叉树的最大深度。
伪代码:
public int getHeight(TreeNode root) {
if(node == null) return 0;
左:int leftheight = getHeight(node.left);
右:int rightHeight == getHeight(node.right);
中:int height = max(leftHeight, rightHeight);
return height;
}
```java
public class depthBT {
public int maxDepth(TreeNode root) {
if(root == null) return 0;
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
int height = Math.max(leftHeight,rightHeight) +1;
return height;
}
}
```
### 6.9 111. Minimum Depth of Binary Tree (use recursion traversal)
Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
Note: A leaf is a node with no children.
```java
public class depthBT {
public int minDepth(TreeNode root) {
if(root == null) return 0;
int leftDepth = minDepth(root.left);
int rightDepth = minDepth(root.right);
//如果只有一个叶子节点不为空,那么就返回该节点的深度+1;
if(root.left != null && root.right == null) return leftDepth+1;
if(root.left == null && root.right != null) return rightDepth+1;
return Math.min(leftDepth,rightDepth) + 1;
}
}