力扣labuladong——一刷day46

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • [一、力扣971. 翻转二叉树以匹配先序遍历](#一、力扣971. 翻转二叉树以匹配先序遍历)
  • [二、力扣987. 二叉树的垂序遍历](#二、力扣987. 二叉树的垂序遍历)
  • [三、力扣666. 路径总和 IV](#三、力扣666. 路径总和 IV)

前言


二叉树的递归分为「遍历」和「分解问题」两种思维模式,这道题需要用到「遍历」的思维。

一、力扣971. 翻转二叉树以匹配先序遍历

java 复制代码
/**
 * Definition for a binary tree node.
 * public 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> res = new ArrayList<>();
    int[] voyageArr;
    int i = 0;
    boolean flag = true;
    public List<Integer> flipMatchVoyage(TreeNode root, int[] voyage) {
        voyageArr = voyage;
        fun(root);
        if(flag == false){
            List<Integer> list = new ArrayList<>();
            list.add(-1);
            return list;
        }
        return res;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        if(root.val != voyageArr[i++]){
            flag = false;
            return;
        }
        if(root.left != null && root.left.val != voyageArr[i]){
            res.add(root.val);
            TreeNode temp = root.left;
            root.left = root.right;
            root.right = temp;
        }
        fun(root.left);
        fun(root.right);
    }
}

二、力扣987. 二叉树的垂序遍历

java 复制代码
/**
 * Definition for a binary tree node.
 * public 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 {
    class Triple{
        TreeNode node;
        int row, col;
        public Triple(TreeNode node, int row, int col){
            this.node = node;
            this.row = row;
            this.col = col;
        }
    }
    LinkedList<List<Integer>> res = new LinkedList<>();
    List<Triple> nodes = new ArrayList<>();
    public List<List<Integer>> verticalTraversal(TreeNode root) {
        traverse(root,0,0);
        Collections.sort(nodes, (tri1,tri2)->{
            if(tri1.col != tri2.col){
                return tri1.col - tri2.col;
            }else if(tri1.row != tri2.row){
                return tri1.row - tri2.row;
            }else{
                return tri1.node.val - tri2.node.val;
            }
        });
        int pre = Integer.MIN_VALUE;
        for(int i = 0; i < nodes.size(); i ++){
            Triple cur = nodes.get(i);
            if(cur.col != pre){
                res.addLast(new LinkedList<Integer>());
                pre = cur.col;
            }
            res.getLast().add(cur.node.val);
        }
        return res;
    }
    public void traverse(TreeNode root, int row, int col){
        if(root == null){
            return ;
        }
        nodes.add(new Triple(root,row,col));
        traverse(root.left, row+1, col-1);
        traverse(root.right, row+1, col + 1);
    }
}

三、力扣666. 路径总和 IV

java 复制代码
class Solution {
    Map<Integer,Integer> tree = new HashMap<>();
    int sum = 0;
    public int pathSum(int[] nums) {
        for(int i = 0; i < nums.length; i ++){
            int value = nums[i]%10;
            int code = nums[i]/10;
            tree.put(code,value);
        }
        int rootCode = nums[0]/10;
        fun(rootCode,0);
        return sum;
    }
    public void fun(int code, int path){
        if(!tree.containsKey(code)){
            return;
        }
        int value = tree.get(code);
        int[] pos = decode(code);
        int depth = pos[0], id = pos[1];
        int leftCode = encode(depth+1, 2*id-1);
        int rightCode = encode(depth+1,2*id);
        if(!tree.containsKey(leftCode) && !tree.containsKey(rightCode)){
            sum += path + value;
            return;
        }
        fun(leftCode, path + value);
        fun(rightCode, path + value);
    }
    public int[] decode(int code){
        int id = code%10;
        int depth = code/10;
        return new int[]{depth,id};
    }
    public int encode(int depth, int id){
        return depth*10 + id;
    }
}
相关推荐
gugugu.1 小时前
数据结构:AVL树
数据结构
大三觉醒push亡羊补牢女娲补天版1 小时前
数据结构之树(4)
数据结构
2401_857439691 小时前
SpringBoot在线教育平台:设计与实现的深度解析
java·spring boot·后端
总是学不会.1 小时前
SpringBoot项目:前后端打包与部署(使用 Maven)
java·服务器·前端·后端·maven
IT学长编程1 小时前
计算机毕业设计 视频点播系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·视频点播系统
极客小张2 小时前
基于STM32的智能家居语音控制系统:集成LD3320、ESP8266设计流程
c语言·stm32·物联网·算法·毕业设计·课程设计·语言识别
一 乐2 小时前
英语词汇小程序小程序|英语词汇小程序系统|基于java的四六级词汇小程序设计与实现(源码+数据库+文档)
java·数据库·小程序·源码·notepad++·英语词汇
曳渔3 小时前
Java-数据结构-反射、枚举 |ू・ω・` )
java·开发语言·数据结构·算法
laocooon5238578863 小时前
java 模拟多人聊天室,服务器与客户机
java·开发语言
风槐啊3 小时前
六、Java 基础语法(下)
android·java·开发语言