0
点赞
收藏
分享

微信扫一扫

【数据结构】二叉树相关oj题(一)

目录

1、二叉树的构建及遍历

1.1、题目介绍

1.2、解题思路

1.3、代码描述

1.4、完整代码

 2、二叉树的层次遍历

2.1、题目介绍

2.2、解题思路

2.3、代码描述

2.4、完整代码 

 

1、二叉树的构建及遍历

1.1、题目介绍

原题链接:KY11 二叉树构建及遍历_牛客题霸_牛客网 (nowcoder.com)

示例1:

1.2、解题思路

1.3、代码描述

class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val) {
        this.val = val;
    }
}
    public static int i = 0;

    public static TreeNode createTree(String str) {
        if(str == null) {
            return null;
        }
        TreeNode root = null;
        char ch = str.charAt(i++);
        if(ch != '#') {
            root = new TreeNode(ch);
            root.left = createTree(str);
            root.right = createTree(str);
        } 
        return root;
    }
    public static void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

1.4、完整代码

import java.util.Scanner;

class TreeNode {
    public char val;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(char val) {
        this.val = val;
    }
}

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        //创建二叉树
        TreeNode root = createTree(str);
        //中序遍历二叉树
        inOrder(root);
    }

    public static int i = 0;

    public static TreeNode createTree(String str) {
        if(str == null) {
            return null;
        }
        TreeNode root = null;
        char ch = str.charAt(i++);
        if(ch != '#') {
            root = new TreeNode(ch);
            root.left = createTree(str);
            root.right = createTree(str);
        } 
        return root;
    }

    
    public static void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }
}

 2、二叉树的层次遍历

2.1、题目介绍

原题链接:102. 二叉树的层序遍历 - 力扣(LeetCode)

 示例1:

示例2:

示例3: 

提示:

2.2、解题思路

2.3、代码描述

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            //此处对cur.val执行打印操作,打印的顺序就是层次遍历顺序
            System.out.print(root.val + " ");
            if(cur.left != null) {
                queue.add(cur.left);
            }
            if(cur.right != null) {
                queue.add(cur.right);
            }
        }

 

2.4、完整代码 

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> retList = new LinkedList<>();
        if(root == null) {
            return retList;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()) {
            List<Integer> list = new LinkedList<>();   //存放一层节点的集合List
            int size = queue.size();   //每次计算队列长度

            while(size --> 0) {   //每完成一次出队,size自减1
                TreeNode cur = queue.poll();
                list.add(cur.val);   
                if(cur.left != null) {
                    queue.add(cur.left);
                }
                if(cur.right != null) {
                    queue.add(cur.right);
                }
            }
            retList.add(list);  //将一层的节点添加到返回集合retList中
        }
        return retList;
    }
}

 

【博主推荐】
【计算机网络】虚拟路由冗余(VRRP)协议原理与配置-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/zzzzzhxxx/article/details/134697735?spm=1001.2014.3001.5502


【JavaSE】基础笔记 - 异常(Exception)-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/zzzzzhxxx/article/details/134564361?spm=1001.2014.3001.5502

 【计算机组成原理】知识点巩固 - 存储器概述_半字地址和字节地址-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/zzzzzhxxx/article/details/134482974?spm=1001.2014.3001.5502

如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!

如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!

如果觉得作者写的不错,求给博主一个大大的点赞支持一下,你们的支持是我更新的最大动力!

 

举报

相关推荐

0 条评论