树的遍历算法题总结(第二十六天)

144. 二叉树的前序遍历

题目

给你二叉树的根节点 root ,返回它节点值的 前序 遍历。

答案

java 复制代码
class Solution {
    List<Integer> res = new ArrayList(); 
    public List<Integer> preorderTraversal(TreeNode root) {
        deal(root);
        return res;
    }
    void deal(TreeNode root){
        if(root==null){
            return;
        }
        res.add(root.val);
        deal(root.left);
        deal(root.right);
    }
}

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 curr = stack.pop();
            res.add(curr.val);
            if(curr.right!=null) stack.push(curr.right);//先放右再放左
            if(curr.left!=null) stack.push(curr.left);
        }
        return res;
    }
}

94. 二叉树的中序遍历

题目

给定一个二叉树的根节点 root ,返回 它的 中序 遍历

答案

java 复制代码
class Solution {
    List<Integer> res = new ArrayList();
    public List<Integer> inorderTraversal(TreeNode root) {
        deal(root);
        return res;
    }
    void deal(TreeNode root){
        if(root==null){
            return;
        }
        deal(root.left);
        res.add(root.val);
        deal(root.right);
    }
}

145. 二叉树的后序遍历

题目

给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历

答案

java 复制代码
class Solution {
    List<Integer> res = new ArrayList();
    public List<Integer> postorderTraversal(TreeNode root) {
        deal(root);
        return res;
    }
    void deal(TreeNode root){
        if(root==null){
            return;
        }
        deal(root.left);
        deal(root.right);
        res.add(root.val);
    }
}

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 curr = stack.pop();
            res.add(curr.val);
            if(curr.left!=null) stack.push(curr.left);
            if(curr.right!=null) stack.push(curr.right);
        }
        Collections.reverse(res);
        return res;
    }
}

102. 二叉树的层序遍历

题目

给你二叉树的根节点 root ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。

答案

java 复制代码
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList();
        if(root==null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList();
            for(int i=0;i<size;i++){
                TreeNode curr = queue.poll();
                list.add(curr.val);
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
            res.add(list);
        }
        return res;
    }
}

107. 二叉树的层序遍历 II

题目

给你二叉树的根节点 root ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

答案

java 复制代码
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res = new LinkedList();
        if(root==null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList();
            for(int i=0;i<size;i++){
                TreeNode curr = queue.poll();
                list.add(curr.val);
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
            res.addFirst(list);
        }
        return res;
    }
}

'

199. 二叉树的右视图

题目

给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

答案

java 复制代码
class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new ArrayList();
        if(root==null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i=0;i<size;i++){
                TreeNode curr = queue.poll();
                if(i==size-1){//判断是不是每一层的最后一个元素
                    res.add(curr.val);
                }
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
        }
        return res;
    }
}

637. 二叉树的层平均值

题目

给定一个非空二叉树的根节点 root , 以数组的形式返回每一层节点的平均值。与实际答案相差 10-5 以内的答案可以被接受。

答案

java 复制代码
class Solution {
    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> res = new ArrayList();
        if(root==null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            double sum = 0;
            for(int i=0;i<size;i++){
                TreeNode curr = queue.poll();
                sum += curr.val;//计算每一层的和
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
            res.add(sum/size);
        }
        return res;
    }
}

429. N 叉树的层序遍历

题目

给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。

树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。

答案

java 复制代码
class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList();
        if(root==null){
            return res;
        }
        Queue<Node> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new ArrayList();
            for(int i=0;i<size;i++){
                Node curr = queue.poll();
                list.add(curr.val);
                for(Node node : curr.children){//遍历所有孩子
                    if(node!=null) queue.offer(node);
                }
            }
            res.add(list);
        }
        return res;
    }
}

515. 在每个树行中找最大值

题目

给定一棵二叉树的根节点 root ,请找出该二叉树中每一层的最大值。

答案

java 复制代码
class Solution {
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> res = new ArrayList();
        if(root==null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            int max = Integer.MIN_VALUE;
            for(int i=0;i<size;i++){
                TreeNode curr = queue.poll();
                max = Math.max(max,curr.val);//计算每一层最大值
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
            res.add(max);
        }
        return res;
    }
}

116. 填充每个节点的下一个右侧节点指针

题目

给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:

复制代码
struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

初始状态下,所有 next 指针都被设置为 NULL

答案

java 复制代码
class Solution {
    public Node connect(Node root) {
        if(root==null){
            return root;
        }
        Queue<Node> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            Node pre = queue.poll();
            if(pre.left!=null) queue.offer(pre.left);
            if(pre.right!=null) queue.offer(pre.right);

            for(int i=1;i<size;i++){
                Node curr = queue.poll();
                pre.next = curr;//将前一个 next 指向当前 
                pre = curr;
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
        }
        return root;
    }
}

117. 填充每个节点的下一个右侧节点指针 II

题目

给定一个二叉树:

复制代码
struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

初始状态下,所有 next 指针都被设置为 NULL

答案

java 复制代码
class Solution {
    public Node connect(Node root) {
        if(root==null){
            return root;
        }
        Queue<Node> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            Node pre = queue.poll();
            if(pre.left!=null) queue.offer(pre.left);
            if(pre.right!=null) queue.offer(pre.right);

            for(int i=1;i<size;i++){
                Node curr = queue.poll();
                pre.next = curr;//将前一个 next 指向当前 
                pre = curr;
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
        }
        return root;
    }
}
相关推荐
二进制person4 分钟前
Java SE--方法的使用
java·开发语言·算法
OneQ66630 分钟前
C++讲解---创建日期类
开发语言·c++·算法
JoJo_Way37 分钟前
LeetCode三数之和-js题解
javascript·算法·leetcode
.30-06Springfield1 小时前
人工智能概念之七:集成学习思想(Bagging、Boosting、Stacking)
人工智能·算法·机器学习·集成学习
凌肖战3 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
weixin_478689763 小时前
十大排序算法汇总
java·算法·排序算法
luofeiju4 小时前
使用LU分解求解线性方程组
线性代数·算法
SKYDROID云卓小助手4 小时前
无人设备遥控器之自动调整编码技术篇
人工智能·嵌入式硬件·算法·自动化·信号处理
ysa0510305 小时前
数论基础知识和模板
数据结构·c++·笔记·算法
GEEK零零七5 小时前
Leetcode 1103. 分糖果 II
数学·算法·leetcode·等差数列