• 力扣labuladong——一刷day46


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


    前言


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

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

    /**
     * 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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

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

    /**
     * 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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    三、力扣666. 路径总和 IV

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
  • 相关阅读:
    单代号搭接网络计划:时间参数的计算
    设计模式乱记
    Python运行过程简单介绍
    vue3-ts-vite:Google 多语言调试 / 翻译
    Netty学习日记一:三大组件
    热门新游 2024 植物大战僵尸杂交版 Mac 版本下载安装详细教程
    JavaScript 62 JavaScript 版本 62.3 JavaScript ES6
    Web APIs:事件高级--DOM事件流及DOM事件流程代码验证
    zemax---艾里斑
    如何创建属于自己的百度百科?这几个创建方法赶紧收藏
  • 原文地址:https://blog.csdn.net/ResNet156/article/details/134551892