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

144. 二叉树的前序遍历

题目

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

答案

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 ,返回 它的 中序 遍历

答案

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 ,返回其节点值的 后序遍历

答案

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 ,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。

答案

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 ,返回其节点值 自底向上的层序遍历 。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

答案

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,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

答案

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 以内的答案可以被接受。

答案

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 值分隔(参见示例)。

答案

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 ,请找出该二叉树中每一层的最大值。

答案

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

答案

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

答案

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;
    }
}

最近更新

  1. TCP协议是安全的吗?

    2024-04-22 15:42:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-22 15:42:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-22 15:42:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-22 15:42:05       20 阅读

热门阅读

  1. sqlalchemy expunge的简单使用

    2024-04-22 15:42:05       15 阅读
  2. Linux命令学习—Apache 服务器(上)

    2024-04-22 15:42:05       14 阅读
  3. sql~ 将一行转为多行

    2024-04-22 15:42:05       16 阅读
  4. node.js常用指令

    2024-04-22 15:42:05       14 阅读
  5. 美国基金会注册

    2024-04-22 15:42:05       17 阅读
  6. AUTOSAR OS Alarm讲解

    2024-04-22 15:42:05       18 阅读
  7. CentOS常见命令

    2024-04-22 15:42:05       14 阅读
  8. MySQL--数据的增删改

    2024-04-22 15:42:05       17 阅读