代码随想录-035期-算法训练营【博客笔记汇总表】-CSDN博客
第六章 二叉树part01今日内容: ● 理论基础
● 递归遍历
● 迭代遍历
● 统一迭代详细布置 理论基础 需要了解 二叉树的种类,存储方式,遍历方式 以及二叉树的定义 文章讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html 递归遍历 (必须掌握)二叉树的三种递归遍历掌握其规律后,其实很简单 题目链接/文章讲解/视频讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E9%80%92%E5%BD%92%E9%81%8D%E5%8E%86.html 迭代遍历 (基础不好的录友,迭代法可以放过)题目链接/文章讲解/视频讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E8%BF%AD%E4%BB%A3%E9%81%8D%E5%8E%86.html 统一迭代 (基础不好的录友,迭代法可以放过)这是统一迭代法的写法, 如果学有余力,可以掌握一下题目链接/文章讲解:https://programmercarl.com/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%BB%9F%E4%B8%80%E8%BF%AD%E4%BB%A3%E6%B3%95.html
目录
理论基础
递归遍历-二叉树的前中后序遍历
迭代遍历
统一迭代
理论基础
- 算法公开课
- 题目分类
- 二叉树的种类
- 二叉树的存储方式
- 二叉树的遍历方式
- 二叉树的定义
- 总结
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;}
}
递归遍历-二叉树的前中后序遍历
/*** 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) {if (root == null) {return new ArrayList<>();}ArrayList<Integer> list = new ArrayList<>();preOrder(root, list);return list;}private void preOrder(TreeNode root, ArrayList<Integer> list) {if (root == null) {return;}list.add(root.val);preOrder(root.left, list);preOrder(root.right, list);}
}
package com.question.solve.leetcode.programmerCarl2._07_binaryTrees;import java.util.ArrayList;
import java.util.List;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;}
}//前序遍历·递归·LC144_二叉树的前序遍历
class Solution1 {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<Integer>();preorder(root, result);return result;}public void preorder(TreeNode root, List<Integer> result) {if (root == null) {return;}result.add(root.val);preorder(root.left, result);preorder(root.right, result);}
}//中序遍历·递归·LC94_二叉树的中序遍历
class Solution2 {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();inorder(root, res);return res;}void inorder(TreeNode root, List<Integer> list) {if (root == null) {return;}inorder(root.left, list);list.add(root.val); // 注意这一句inorder(root.right, list);}
}//后序遍历·递归·LC145_二叉树的后序遍历
class Solution3 {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();postorder(root, res);return res;}void postorder(TreeNode root, List<Integer> list) {if (root == null) {return;}postorder(root.left, list);postorder(root.right, list);list.add(root.val); // 注意这一句}
}
迭代遍历
package com.question.solve.leetcode.programmerCarl2._07_binaryTrees;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Stack;public class _0000_迭代遍历 {
}//前序遍历顺序:中-左-右,入栈顺序:中-右-左
class Solution001 {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null) {return result;}Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()) {TreeNode node = stack.pop();result.add(node.val);if (node.right != null) {stack.push(node.right);}if (node.left != null) {stack.push(node.left);}}return result;}
}//中序遍历顺序: 左-中-右,入栈顺序:左-右
class Solution002 {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null) {return result;}Stack<TreeNode> stack = new Stack<>();TreeNode cur = root;while (cur != null || !stack.isEmpty()) {if (cur != null) {stack.push(cur);cur = cur.left;} else {cur = stack.pop();result.add(cur.val);cur = cur.right;}}return result;}
}//后序遍历顺序 左-右-中,入栈顺序:中-左-右,出栈顺序:中-右-左,最后翻转结果
class Solution003 {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null) {return result;}Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()) {TreeNode node = stack.pop();result.add(node.val);if (node.left != null) {stack.push(node.left);}if (node.right != null) {stack.push(node.right);}}Collections.reverse(result);return result;}
}
统一迭代
package com.question.solve.leetcode.programmerCarl2._07_binaryTrees;import java.util.LinkedList;
import java.util.List;
import java.util.Stack;public class _0000_迭代遍历2 {
}class Solution01 {//迭代法-前序遍历代码-如下public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();Stack<TreeNode> st = new Stack<>();if (root != null) st.push(root);while (!st.empty()) {TreeNode node = st.peek();if (node != null) {st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中if (node.right != null) st.push(node.right); // 添加右节点(空节点不入栈)if (node.left != null) st.push(node.left); // 添加左节点(空节点不入栈)st.push(node); // 添加中节点st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。} else { // 只有遇到空节点的时候,才将下一个节点放进结果集st.pop(); // 将空节点弹出node = st.peek(); // 重新取出栈中元素st.pop();result.add(node.val); // 加入到结果集}}return result;}
}class Solution02 {//迭代法-中序遍历代码-如下public List<Integer> inorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();Stack<TreeNode> st = new Stack<>();if (root != null) st.push(root);while (!st.empty()) {TreeNode node = st.peek();if (node != null) {st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中if (node.right != null) st.push(node.right); // 添加右节点(空节点不入栈)st.push(node); // 添加中节点st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。if (node.left != null) st.push(node.left); // 添加左节点(空节点不入栈)} else { // 只有遇到空节点的时候,才将下一个节点放进结果集st.pop(); // 将空节点弹出node = st.peek(); // 重新取出栈中元素st.pop();result.add(node.val); // 加入到结果集}}return result;}
}class Solution03 {//迭代法-后序遍历代码-如下public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new LinkedList<>();Stack<TreeNode> st = new Stack<>();if (root != null) st.push(root);while (!st.empty()) {TreeNode node = st.peek();if (node != null) {st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中st.push(node); // 添加中节点st.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。if (node.right != null) st.push(node.right); // 添加右节点(空节点不入栈)if (node.left != null) st.push(node.left); // 添加左节点(空节点不入栈)} else { // 只有遇到空节点的时候,才将下一个节点放进结果集st.pop(); // 将空节点弹出node = st.peek(); // 重新取出栈中元素st.pop();result.add(node.val); // 加入到结果集}}return result;}
}