美文网首页
代码随想录算法训练营第18天|二叉树part05

代码随想录算法训练营第18天|二叉树part05

作者: pangzhaojie | 来源:发表于2023-05-27 11:05 被阅读0次

二叉树左下角的值

题目链接

https://programmercarl.com/0513.%E6%89%BE%E6%A0%91%E5%B7%A6%E4%B8%8B%E8%A7%92%E7%9A%84%E5%80%BC.html

思路

递归法,需要回溯深度,只有深度最大值有变化时才会记录左下角

    private int maxDepth = 0;
private int res = 0;
public int findBottomLeftValue(TreeNode root) {
    
     find(root, 1);
     return res;
}

public void find(TreeNode node, int depth) {
    if(node.left == null && node.right == null) {
        if(depth > maxDepth) {
            maxDepth = depth;
            res =  node.val;
        }
    }
    if(node.left != null) {
        find(node.left, depth + 1);
    }
    if(node.right != null) {
    find(node.right, depth + 1);

    }
    
}

路径总和

题目链接

https://programmercarl.com/0112.%E8%B7%AF%E5%BE%84%E6%80%BB%E5%92%8C.html

思路

    private boolean hasPathSum =false;
public boolean hasPathSum(TreeNode root, int targetSum) {
    if(root == null) {
        return false;
    }
    hasPathSu(root, targetSum);
    return hasPathSum;

}

private void hasPathSu(TreeNode node, int targetSum) {
    if(node.left == null && node.right == null) {
        if(!hasPathSum) {
            hasPathSum = targetSum == node.val;
        }
    }
    if(node.left!= null) {
        hasPathSu(node.left, targetSum - node.val);
    }
    if(node.right != null) {
        hasPathSu(node.right, targetSum - node.val);
    }
}


    private List<List<Integer>> res = new ArrayList();
private List<Integer> path = new ArrayList();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root ==null) {
            return res;
        }
        pathSum1(root, targetSum);
        return res;
    }

    private void pathSum1(TreeNode root, int targetSum) {
        path.add(root.val);
        if(root.left == null && root.right == null) {
            if(root.val == targetSum) {
                res.add(new ArrayList<>(path));
            }
        }
        if(root.left != null) {
            pathSum1(root.left, targetSum - root.val);
            path.remove(path.size() - 1);
        }
        if(root.right != null) {
            pathSum1(root.right, targetSum - root.val);
            path.remove(path.size() - 1);

        }
    }

构造二叉树

思路挺简单的,记住区间左闭右开 保持这一原则

    private Map<Integer, Integer> map = new HashMap();
public TreeNode buildTree(int[] inorder, int[] postorder) {
    for(int i = 0; i < inorder.length; i++) {
        map.put(inorder[i], i);
    }
    return buildTree1(inorder, 0, inorder.length, postorder, 0, postorder.length);
}

private TreeNode buildTree1(int[] inorder, int inBegin, int inEnd, int[] postorder, int poBegin, int poEnd) {
    if(inBegin == inEnd) {
        return null;
    }
    int mid = map.get(postorder[poEnd - 1]);
    TreeNode root = new TreeNode(inorder[mid]);
    root.left = buildTree1(inorder, inBegin, mid, postorder, poBegin, poBegin + mid - inBegin);
    root.right = buildTree1(inorder, mid+1, inEnd, postorder, poBegin + mid - inBegin, poEnd-1);
    return root;
}

相关文章

  • 【算法题】递归求二叉树深度

    二叉树的深度算法,是二叉树中比较基础的算法了。对应 LeetCode 第104题。 然后你会发现 LeetCode...

  • 数据结构之树的相关问题

    实验要求 实现二叉树的抽象数据类型 实现二叉树的建立的运算 实现二叉树的遍历运算 实现创建哈夫曼树的算法 实验代码...

  • 每日Leetcode—算法(10)

    100.相同的树 算法: 101.对称二叉树 算法: 104.二叉树的最大深度 算法: 107.二叉树的层次遍历 ...

  • 每日Leetcode—算法(11)

    110.平衡二叉树 算法: 111.二叉树的最小树深 算法: 112.路径总和 算法:

  • 我对递归的三重理解

    第一重:二叉树的先序遍历 递归代码: 第二重:从二叉树的角度理解递归算法 举例:Generate Parenthe...

  • 二叉树 for iOS (Swift篇)

    学习了二叉树的概念及二叉树算法习题后, 结合学习的知识通过代码实现一些功能。 二叉树说白了是考验程序员的递归思维,...

  • Algorithm小白入门 -- 二叉树

    二叉树二叉树构造二叉树寻找重复子树 1. 二叉树 基本二叉树节点如下: 很多经典算法,比如回溯、动态规划、分治算法...

  • 深入浅出二叉树遍历的非递归算法 2019-11-15(未经允许,

    1、二叉树遍历的递归算法 递归实现二叉树的遍历非常直观,回顾一下递归的代码: 前序遍历 中序遍历 后序遍历 他们的...

  • 二叉树的基本算法

    二叉树的基本算法 树、二叉树 的基本概念,参考数据结构算法之美-23讲二叉树基础(上):树、二叉树[https:/...

  • 二叉树的遍历

    前言 二叉树和链表在历年春招笔试中,都是重点考核对象。链表由于算法简单,一般考代码实现能力。二叉树考核遍历。 二叉...

网友评论

      本文标题:代码随想录算法训练营第18天|二叉树part05

      本文链接:https://www.haomeiwen.com/subject/mkwledtx.html