【算法刷题day18】Leetcode:513. 找树左下角的值、112. 路径总和、113. 路径总和ii、106.从中序与后序遍历序列构造二叉树、105.从前序与中序遍历序列构造二叉树

草稿图网站
java的Deque

Leetcode 513. 找树左下角的值

题目:513. 找树左下角的值
解析:代码随想录解析

解题思路

迭代法:层序遍历每层第一个。
递归法:使用回溯的思想。

代码

/**
 * 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 {
    public int findBottomLeftValue(TreeNode root) {
        int res = root.val;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            TreeNode node = queue.poll();
            res = node.val;
            if (node.left != null)  queue.add(node.left);
            if (node.right != null)  queue.add(node.right);
            for (int i = 1; i < size; i++) {
                node = queue.poll();
                if (node.left != null)  queue.add(node.left);
                if (node.right != null)  queue.add(node.right);
            }
        }
        return res;
    }
}

//回溯
class Solution {
    private int maxDepth = 0;
    private int res = 0;
    public int findBottomLeftValue(TreeNode root) {
        traversal(root, 1);
        return res;
    }
    private void traversal(TreeNode node, int depth) {
        if (node.left == null && node.right == null){
            if (depth > maxDepth){
                maxDepth = depth;
                res = node.val;
            }
        }
        if (node.left != null)
            traversal(node.left, depth + 1);
        if (node.right != null)
            traversal(node.right, depth + 1);
    }
}

总结

迭代法使用层序遍历即可,但太慢了

Leetcode 112. 路径总和

题目:112. 路径总和
解析:代码随想录解析

解题思路

代码

/**
 * 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 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null)
            return false;
        return traversal(root, targetSum, root.val);
    }
    private boolean traversal(TreeNode node, int targetSum, int curSum) {
        if (node.left == null && node.right == null && curSum == targetSum)
            return true;
        if (node.left == null && node.right == null)
            return false;
        if (node.left != null)
            if (traversal(node.left, targetSum, curSum + node.left.val))
                return true;
        if (node.right != null)
            if (traversal(node.right, targetSum, curSum + node.right.val))
                return true;
        return false;
    }
}

//简化版回溯
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null)
            return false;
        if (root.left == null && root.right == null && targetSum == root.val)
            return true;
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }
}

//前序遍历
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null)
            return false;
        Stack<TreeNode> stackTN = new Stack<TreeNode>();
        Stack<Integer> stackInt = new Stack<Integer>();
        stackTN.push(root);
        stackInt.push(root.val);
        while (!stackTN.isEmpty()){
            TreeNode node = stackTN.pop();
            int curSum = stackInt.pop();
            if (node.left == null && node.right == null && curSum == targetSum)
                return true;
            if (node.left != null){
                stackTN.push(node.left);
                stackInt.push(node.left.val + curSum);
            }
            if (node.right != null){
                stackTN.push(node.right);
                stackInt.push(node.right.val + curSum);
            }
        }
        return false;
    }
}

总结

暂无

Leetcode 113. 路径总和ii

题目:113. 路径总和ii
解析:代码随想录解析

解题思路

和上面112.的判断逻辑一致

代码

/**
 * 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 {
    private List<List<Integer>> res = new ArrayList<List<Integer>>();
    private List<Integer> paths = new ArrayList<Integer>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if (root == null)
            return res;
        traversal(root, targetSum);
        return res;
    }
    private void traversal(TreeNode node, int sum) {
        paths.add(node.val);
        if (node.left == null && node.right == null){
            if(sum == node.val)
                res.add(new ArrayList<Integer>(paths));
            return;
        }
        if (node.left != null){
            // paths.add(node.left.val);
            traversal(node.left, sum - node.val);
            paths.remove(paths.size() - 1);
        }
        if (node.right != null){
            // paths.add(node.right.val);
            traversal(node.right, sum - node.val);
            paths.remove(paths.size() - 1);
        }
    }
}

总结

res.add(new ArrayList< Integer >(paths));错写成了res.add(paths);
这会导致每次传入res的是paths在堆中的索引,而随着paths的改变,res中最终的元素也会改变,所以不能正确存储结果,所以得新建一个ArrayList< Integer >来保证结果是不变的。

Leetcode 106.从中序与后序遍历序列构造二叉树

题目:106.从中序与后序遍历序列构造二叉树
解析:代码随想录解析

解题思路

划分划分

代码

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if (inorder.length == 0 || postorder.length == 0)
            return null;
        return buildNode(inorder, 0, inorder.length, postorder, 0, postorder.length);
    }
    private TreeNode buildNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
        if (inBegin == inEnd)
            return null;
        TreeNode root = new TreeNode(postorder[postEnd - 1]);
        if (inBegin + 1 == inEnd)
            return root;
        int midIndex = -1;
        for (int i = inBegin; i < inEnd; i++)
            if (inorder[i] == root.val)
                midIndex = i;
        root.left = buildNode(inorder, inBegin, midIndex, postorder, postBegin, postBegin + (midIndex - inBegin));
        root.right = buildNode(inorder, midIndex+1, inEnd, postorder, postBegin + (midIndex - inBegin), postEnd - 1);
        return root;
    }
}

总结

划分划分

Leetcode 105.从前序与中序遍历序列构造二叉树

题目:105.从前序与中序遍历序列构造二叉树
解析:代码随想录解析

解题思路

划分划分

代码

/**
 * 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 {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if (preorder.length == 0 || inorder.length == 0)
            return null;
        return buildNode(preorder, 0, preorder.length, inorder, 0, inorder.length);
    }
    private TreeNode buildNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
        if (inBegin == inEnd)
            return null;
        TreeNode root = new TreeNode(preorder[preBegin]);
        if (inBegin + 1 == inEnd)
            return root;
        int midIndex = -1;
        for (int i = inBegin; i < inEnd; i++)
            if (inorder[i] == root.val)
                midIndex = i;
        root.left = buildNode(preorder, preBegin + 1, preBegin + (midIndex - inBegin) + 1, inorder, inBegin, midIndex);
        root.right = buildNode(preorder, preBegin + (midIndex - inBegin) + 1, preEnd, inorder, midIndex + 1, inEnd);
        return root;
    }
}

总结

划分划分

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
给定一个整数数组 nums 和一个目标 target,要求在数组出两个数的和等于目标,并返回这两个数的索引。 思路1:暴力法 最简单的思路是使用两层循环遍数组的所有组合,判断两个数的和是否等于目标。如果等于目标,则返回这两个数的索引。 此方法的时间复杂度为O(n^2),空间复杂度为O(1)。 思路2:哈希表 为了优化时间复杂度,可以使用哈希表来存储数组的元素和对应的索引。遍数组,对于每个元素nums[i],我们可以通过计算target - nums[i]的,查哈希表是否存在这个差。 如果存在,则说明到了两个数的和等于目标,返回它们的索引。如果不存在,将当前元素nums[i]和它的索引存入哈希表。 此方法的时间复杂度为O(n),空间复杂度为O(n)。 思路3:双指针 如果数组已经排,可以使用双指针的方法来求解。假设数组从小到大排,定义左指针left指向数组的第一个元素,右指针right指向数组的最后一个元素。 如果当前两个指针指向的数的和等于目标,则返回它们的索引。如果和小于目标,则将左指针右移一位,使得和增大;如果和大于目标,则将右指针左移一位,使得和减小。 继续移动指针,直到到两个数的和等于目标或者左指针超过了右指针。 此方法的时间复杂度为O(nlogn),空间复杂度为O(1)。 以上三种方法都可以解决问,选择合适的方法取决于具体的应用场景和要求。如果数组规模较小并且不需要考虑额外的空间使用,则暴力法是最简单的方法。如果数组较大或者需要优化时间复杂度,则哈希表或双指针方法更合适。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Allmight_Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值