一、前序遍历 144
https://www.cnblogs.com/zhi-leaf/p/10813048.html
1.原理
前序遍历的访问顺序:先根节点,再左子树,最后右子树;上图的访问结果为:GDAFEMHZ。
注:前序遍历会沿着根节点的左节点找到底,没找到然后再沿着右节点的左节点找到底,然后自底向上返回右节点
2.代码
/**
* 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 List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if (root == null) return res;
TreeNode cur = root;
Stack<TreeNode> stack = new Stack<>();
while(cur!=null || !stack.isEmpty()){
while(cur!=null){
//栈里存储的就是谦虚连理的结果,所以只要跟着栈一步步将结果存到List里就可以了
res.add(cur.val);
stack.add(cur);
cur = cur.left;
}
cur = stack.pop();
cur = cur.right;
}
return res;
}
}
二、中序遍历 94
1.原理
中序遍历的访问顺序:先左子树,再根节点,最后右子树;上图的访问结果为:ADEFGHMZ。
2.代码
/**
* 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 List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new LinkedList<>();
if (root == null) return res;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur =root;
while(cur!=null || !stack.isEmpty()){
//入栈,依次加入cur和对应的cur.left
while(cur!=null){
stack.add(cur);
cur = cur.left;
}
//加元素,回退
cur = stack.pop();
res.add(cur.val);
cur = cur.right;
}
return res;
}
}
三、后序遍历 145
1.原理
后序遍历的访问顺序:先左子树,再右子树,最后根节点,上图的访问结果为:AEFDHZMG。
2.代码
https://www.bilibili.com/video/BV1mC4y147Q5?from=search&seid=6558741197542271320&spm_id_from=333.337.0.0
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List <Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
TreeNode pre =null;
while (!stack.isEmpty() || root !=null){
if (root !=null){
stack.push(root);
root=root.left;
}else{
TreeNode peek = stack.peek();
if (peek.right == null || pre == peek.right ) {
list.add(peek.val);
pre = stack.pop();
}else{
root = peek.right;
}
}
}
return list;
}
}
四、层次遍历 102. 二叉树的层序遍历
1.原理
层次遍历的访问结果:GDMAFHZE
2.代码
/**
* 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 List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
//base case
if (root == null) return res;
//中间变量
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
//
while(!queue.isEmpty()){
ArrayList<Integer> temp = new ArrayList<>();
int size = queue.size();
for(int i =1; i<=size;i++){
TreeNode cur = queue.poll();
temp.add(cur.val);
if (cur.left!=null) queue.add(cur.left);
if (cur.right!=null) queue.add(cur.right);
}
res.add(temp);
}
return res;
}
}
五、锯齿遍历 103. 二叉树的锯齿形层序遍历
1.题目
https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/
2.思路
广度优先算法(BFS)h和深度优先算法(DFS)的原理及实现见:https://blog.csdn.net/Xiao__Bei/article/details/121645498
同一加到一个队列里,啊然后用双端队列实现不同方向的取操作
3.代码
/**
* 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 List<List<Integer>> zigzagLevelOrder(TreeNode root) {
//层序遍历+双端队列
List<List<Integer>> res = new ArrayList<List<Integer>>();
if (root == null) return res;
Queue<TreeNode> queue =new LinkedList<TreeNode>();
queue.offer(root);
boolean judge = true;
while(!queue.isEmpty()){
int size = queue.size();
//List<Integer> temp = new ArrayList<>();
Deque<Integer> deque = new LinkedList<>();
for (int i = 1; i<=size; i++){
TreeNode cur = queue.poll();
if(judge){
//向队尾加元素
deque.offerLast(cur.val);
}else{
//往队头加元素
deque.offerFirst(cur.val);
}
if(cur.left!=null) queue.offer(cur.left);
if(cur.right!=null) queue.offer(cur.right);
}
res.add(new LinkedList<Integer>(deque));
judge = !judge;
}
return res;
}
}