通过队列来实现,动画如下:
大概就掌握了个迭代法吧,先这样8
// 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);
}
}
}
102.二叉树的层序遍历
List<List<Integer>> res = new ArrayList<List<Integer>>();
public void levelCheck(TreeNode root) {
if (root == null) return;
Deque<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
List<Integer> list = new ArrayList<Integer>();
int len = queue.size();
while (len > 0) {
TreeNode tmp = queue.poll();
list.add(tmp.val);
if (tmp.left != null) queue.offer(tmp.left);
if (tmp.right != null) queue.offer(tmp.right);
len--;
}
res.add(list);
}
}
public List<List<Integer>> levelOrder(TreeNode root) {
if(root == null) return res;
else {
levelCheck(root);
return res;
}
}
题目链接:226. 翻转二叉树 - 力扣(LeetCode)
思路就是简单的利用递归,将左右节点交换即可
ac代码如下:
public void change(TreeNode root){
if(root == null) return;
TreeNode tmp = root.right;
root.right = root.left;
root.left = tmp;
change(root.left);
change(root.right);
}
public TreeNode invertTree(TreeNode root) {
change(root);
return root;
}
层序法遍历使用之前的方法即可
public TreeNode invertTree(TreeNode root) {
Deque<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
TreeNode tmp = queue.poll();
if (tmp != null) {
TreeNode swapNode = tmp.left;
tmp.left = tmp.right;
tmp.right = swapNode;
queue.offer(tmp.left);
queue.offer(tmp.right);
}
}
return root;
}
}
前中后序遍历还是下次再看8
题目链接:101. 对称二叉树 - 力扣(LeetCode)
初见思路:原本的想法是直接遍历左子树 右子树的,分别使用两个List来保存其中内容,最后进行对比,若是不相同则返回false,但这样做其实是不可行的,由于有null节点的存在,除非在List中也加入null,否则无法判断是否相同
递归法:
老生常谈的递归法的使用步骤了:
- 确定传入参数以及返回值
- 确定终止条件
- 确定单次递归中的处理逻辑
本次书写同样遵循这个原则:
递归法ac代码如下:
public boolean compare(TreeNode left,TreeNode right){
if(left == null && right == null)
{
return true;
}
else if(left == null && right!= null) return false;
else if(right == null && left!= null) return false;
else if(left.val != right.val) return false;
else{
boolean compareOut = compare(left.left,right.right);
boolean compareIn = compare(left.right,right.left);
return compareIn && compareOut;
}
}
public boolean isSymmetric(TreeNode root) {
return compare(root.left,root.right);
}
迭代法代码看了看,大体思路为使用队列,初始化加入左右节点,随后每次迭代加入左节点的左侧右节点右侧 左节点右侧 右节点左侧,并进行条件判断