LeetCode刷题记--- 437 / 144

1. 每日记

从前天开始就参加了那个人工智能大赛,选择深度学习方向的比赛,从零开始学python和机器学习,Keras😂感觉有点难,还需要线代和求导的知识,以前每太学好,裂开了呀,由于这个原因,导致个人LeetCode刷题进度可能会受到影响,但是每日一题还是必做,但是博客可能就没那么频繁更新了。

2.LeetCode每日一题

给定一个二叉树的根节点 root ,和一个整数 targetSum ,求该二叉树里节点值之和等于 targetSum 的 路径 的数目。

路径 不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
在这里插入图片描述


import LeetCode.TreeNode;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class Solution437 {
    //给定一个二叉树的根节点 root,和一个整数 targetSum ,求该二叉树里节点值之和等于 targetSum 的 路径 的数目。
    //
    //路径 不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。
    //
    //层次遍历 + 回溯
    public int pathSum(TreeNode root, int targetSum) {
        if(root == null){
            return 0;
        }
        int count = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){       //层次遍历
            int size = queue.size();
            for(int i = 0;i < size;i++){
                TreeNode node = queue.poll();
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
                count += find(node,targetSum);
            }
        }

        return count;
    }
    int find(TreeNode root,int target){
        int countTargetTimes = 0;
        Stack<TreeNode> stack = new Stack<>();
        Stack<Integer> stackVal = new Stack<>();
        stack.push(root);
        stackVal.push(root.val);
        int sum = 0;
        TreeNode node = root;
        while (!stack.isEmpty()){   //先序遍历
            while (node != null){
                sum += node.val;
                stackVal.push(sum);
                if(sum == target){
                    countTargetTimes++;
                }
                stack.push(node);
                node = node.left;
            }
            node = stack.pop();
            sum = stackVal.pop();
            node = node.right;
        }
        return countTargetTimes;
    }
}

这题思路方面应该不难,看到题目基本就有思路了,但是代码却码了很久,主要是迭代的形式遍历不熟练,自己还重新去思考了迭代方式先序遍历树,
额。。。本来可以用递归的形式,但是需要全局变量就没这样做了,这题我自己写的上面的代码不是很好,时间和空间复杂度都很高,对于时间复杂度我可以进行优化,有思路而且还很不错,但是代码实现有点难,,,,加上比较晚了,就没去继续了,看官方题解去了。。。

3. 144 二叉树先序遍历

在这里插入图片描述


import LeetCode.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Solution144 {
    public List<Integer> preorderTraversal(TreeNode root) {
//        List<Integer> list = new ArrayList<>();
//        if(root == null){
//            return list;
//        }
//        Stack<TreeNode> stack = new Stack<>();
//        stack.push(root);
//        while (!stack.isEmpty()){
//            int size = stack.size();
//            for(int i = 0;i < size;i++){
//                TreeNode node = stack.pop();
//                if(node.right != null){
//                    stack.push(node.right);
//                }
//                if (node.left != null){
//                    stack.push(node.left);
//                }
//                list.add(node.val);
//            }
//        }
//        return list;
        //第二种方法
        List<Integer> list = new ArrayList<>();
        if(root == null){
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        TreeNode node = root;
        while (!stack.isEmpty()){
            while (node != null){
                list.add(node.val);
                stack.push(node);
                node = node.left;
            }
            node = stack.pop();
            node = node.right;
        }
        return list;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值