二叉树的层序遍历、翻转二叉树、对称二叉树
二叉树的层序遍历
队列先进先出,符合层序遍历的逻辑。
栈先进后出适合模拟深度优先遍历也就是递归的逻辑。
图解步骤
代码
// 102.二叉树的层序遍历
class Solution {
public List<List<Integer>> resList = new ArrayList<List<Integer>>();
public List<List<Integer>> levelOrder(TreeNode root) {
//checkFun01(root,0);
checkFun02(root);
return resList;
}
//DFS--递归方式
public void checkFun01(TreeNode node, Integer deep) {
if (node == null) return;
deep++;
if (resList.size() < deep) {
//当层级增加时,list的Item也增加,利用list的索引值进行层级界定
List<Integer> item = new ArrayList<Integer>();
resList.add(item);
}
resList.get(deep - 1).add(node.val);
checkFun01(node.left, deep);
checkFun01(node.right, deep);
}
//BFS--迭代方式--借助队列
public void checkFun02(TreeNode node) {
if (node == null) return;
Queue<TreeNode> que = new LinkedList<TreeNode>();
que.offer(node);
while (!que.isEmpty()) {
List<Integer> itemList = new ArrayList<Integer>();
int len = que.size();
while (len > 0) {
TreeNode tmpNode = que.poll();
itemList.add(tmpNode.val);
if (tmpNode.left != null) que.offer(tmpNode.left);
if (tmpNode.right != null) que.offer(tmpNode.right);
len--;
}
resList.add(itemList);
}
}
}
翻转二叉树
力扣连接:226. 翻转二叉树(简单)
1.递归的方法
递归的图解步骤
递归代码
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
invert(root);
return root;
}
public void invert(TreeNode root){
if(root==null){
return;
}
invert(root.left);
invert(root.right);
TreeNode leftNode = root.left;
root.left = root.right;
root.right = leftNode;
}
}
2.迭代的方法
使用栈的方法
代码
class Solution {
public TreeNode invertTree(TreeNode root) {
if(null==root) return root;
Stack<TreeNode> st = new Stack<>();
st.push(root);
while(!st.isEmpty()){
TreeNode node = st.pop();
//交换
TreeNode tmp = node.left;
node.left = node.right;
node.right = tmp;
if(null!=node.right) st.push(node.right);
if(null!=node.left) st.push(node.left);
}
return root;
}
}
对称二叉树
力扣连接:101. 对称二叉树(简单)
1.递归的方法
使用两棵树,leftTree和rightTree
递归的图解步骤
递归代码
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return compare(root.left, root.right);
}
public boolean compare(TreeNode leftTree, TreeNode rightTree){
if(leftTree==null&&rightTree==null) return true;
if(leftTree==null&&rightTree!=null) return false;
if(leftTree!=null&&rightTree==null) return false;
if(leftTree.val!=rightTree.val) return false;
return compare(leftTree.left, rightTree.right)&&compare(leftTree.right, rightTree.left);
}
}
2.迭代的方法
使用队列的方法
迭代(队列)的图解步骤
通过队列来判断根节点的左子树和右子树的内侧和外侧是否相等。
代码
/**
* 迭代法
* 使用普通队列
*/
public boolean isSymmetric3(TreeNode root) {
Queue<TreeNode> deque = new LinkedList<>();
deque.offer(root.left);
deque.offer(root.right);
while (!deque.isEmpty()) {
TreeNode leftNode = deque.poll();
TreeNode rightNode = deque.poll();
if (leftNode == null && rightNode == null) {
continue;
}
// 三个判断条件合并
if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {
return false;
}
// 这里顺序与使用Deque不同
deque.offer(leftNode.left);
deque.offer(rightNode.right);
deque.offer(leftNode.right);
deque.offer(rightNode.left);
}
return true;
}