目录
题目1: 二叉树的递归遍历(前、中、后)
- 题目链接1:144. 二叉树的前序遍历
- 题目链接2:94. 二叉树的中序遍历
- 题目链接3:145. 二叉树的后序遍历
1- 思路
- 递归遍历实现较为简单,实现一个递归函数即可
2- 题解
①递归——前序
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root==null){
return res;
}
// 前序:中、左、右
res.add(root.val);
preorderTraversal(root.left);
preorderTraversal(root.right);
return res;
}
}
②递归——中序
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root==null){
return res;
}
// 中序
inorderTraversal(root.left);
res.add(root.val);
inorderTraversal(root.right);
return res;
}
}
②递归——后序
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root==null){
return res;
}
postorderTraversal(root.left);
postorderTraversal(root.right);
res.add(root.val);
return res;
}
}
题目2: 二叉树的迭代遍历(前、中、后)
- 题目链接1:144. 二叉树的前序遍历
- 题目链接2:94. 二叉树的中序遍历
- 题目链接3:145. 二叉树的后序遍历
1- 思路
- 因为是迭代遍历,因此需要借助额外的数据结构 栈 来实现
2- 题解
①迭代——前序
因为借助了栈实现迭代遍历,因此 前序的规则是 中、左、右,则入栈的顺序为 中、右、左
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if(root==null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
res.add(node.val);
if(node.right!=null){
stack.push(node.right);
}
if(node.left!=null){
stack.push(node.left);
}
}
return res;
}
}
②迭代——中序
中序遍历,即 左、右
- 对于迭代情况下的 中序遍历而言,其思路是借助一个 cur 指针,只要当前指针所指向的不为 空 或 栈内不为空则一直迭代
- 如果 cur 非空,则一直向左移动
- 否则 弹出 栈中的元素,弹出后 收集结果,并向右移动。
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if(root==null){
return res;
}
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();
res.add(cur.val);
cur = cur.right;
}
}
return res;
}
}
③迭代——后续
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if(root==null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
res.add(node.val);
if(node.left!=null){
stack.push(node.left);
}
if(node.right!=null){
stack.push(node.right);
}
}
Collections.reverse(res);
return res;
}
}
题目3: 二叉树的统一迭代 (前、中、后)
- 题目链接1:144. 二叉树的前序遍历
- 题目链接2:94. 二叉树的中序遍历
- 题目链接3:145. 二叉树的后序遍历
1- 思路
- 二叉树的统一迭代法,其中模板较为一致,需要改变的逻辑为
node!= null
时候的处理逻辑
2- 题解
① 前序
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root==null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.peek();
if(node!=null){
// 加入顺序
stack.pop();
if(node.right!=null) stack.push(node.right);
if(node.left!=null) stack.push(node.left);
stack.push(node);
stack.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
}
else{
stack.pop();
node = stack.peek();
stack.pop();
res.add(node.val);
}
}
return res;
}
}
② 中序
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root==null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.peek();
if(node!=null){
// 加入顺序
stack.pop();
if(node.right!=null) stack.push(node.right);
stack.push(node);
stack.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
if(node.left!=null) stack.push(node.left);
}
else{
stack.pop();
node = stack.peek();
stack.pop();
res.add(node.val);
}
}
return res;
}
}
③ 后序
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root==null){
return res;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.peek();
if(node!=null){
// 加入顺序
stack.pop();
stack.push(node);
stack.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。
if(node.right!=null) stack.push(node.right);
if(node.left!=null) stack.push(node.left);
}
else{
stack.pop();
node = stack.peek();
stack.pop();
res.add(node.val);
}
}
return res;
}
}