【LeeCode】⼆叉树的序遍历
原创
©著作权归作者所有:来自51CTO博客作者51玖拾柒的原创作品,请联系作者获取转载授权,否则将追究法律责任
144.⼆叉树的前序遍历
【题目描述】
给你二叉树的根节点 root
,返回它节点值的 前序 遍历。
【示例】
【递归法】
package com.company;
import java.util.ArrayList;
import java.util.List;
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 {
List<Integer> list = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
// 前序: 根左右
if (root == null) return list;
preOrder(root);
return list;
}
public void preOrder(TreeNode root){
if (root == null) return;
list.add(root.val);
preOrder(root.left);
preOrder(root.right;
}
}
public class Test {
public static void main(String[] args) {
}
}
【迭代法】
前序遍历是中左右,每次先处理的是中间节点,那么先将根节点放入栈中,然后将右孩子加入栈,再加入左孩子。
class Solution {
// 前序(中左右) --> 入栈: 中右左
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null) return list;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.empty()){
TreeNode node = stack.pop();
list.add(node.val);
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
}
return list;
}
}
public class Test {
public static void main(String[] args) {
}
}
145.二叉树的后序遍历
【递归法】
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) return list;
postOrder(root);
return list;
}
// 后序: 左右根
public void postOrder(TreeNode root){
if (root == null) return;
postOrder(root.left);
postOrder(root.right);
list.add(root.val);
}
}
【迭代法】
class Solution {
// 后序遍历顺序 左-右-中 入栈顺序:中-左-右 出栈顺序:中-右-左, 最后翻转结果
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.empty()){
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;
}
}
94.⼆叉树的中序遍历
【递归法】
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if (root == null) return list;
postOrder(root);
return list;
}
// 中序: 左根右
public void postOrder(TreeNode root){
if (root == null) return;
postOrder(root.left);
list.add(root.val);
postOrder(root.right);
}
}
【迭代法】
// 中序遍历顺序: 左-中-右 入栈顺序: 左-右
class Solution {
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;
}
}
二叉树层序遍历
515.在每个树行中找最大值
116.填充每个节点的下一个右侧节点指针
117.填充每个节点的下一个右侧节点指针II
104.二叉树的最大深度
111.二叉树的最小深度
翻转二叉树
学习参考
https://leetcode.cn/problems/binary-tree-preorder-traversal/solutions/461821/er-cha-shu-de-qian-xu-bian-li-by-leetcode-solution/
https://leetcode.cn/problems/binary-tree-preorder-traversal/solutions/87526/leetcodesuan-fa-xiu-lian-dong-hua-yan-shi-xbian-2/