创造营第十八天 | ● 513.找树左下角的值● 112. 路径总和 113.路径总和ii● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树

找树左下角的值  

题目链接/文章讲解/视频讲解:代码随想录

递归

class Solution {
    int Deep = -1;
    int value;
    public int findBottomLeftValue(TreeNode root) {
        value = root.val;
        findLeftValue(root, 0);
        return value;
    }
    public void findLeftValue(TreeNode root, int deep) {
        if(root == null) return;
        if(root.left == null && root.right == null) {//叶子结点处理
            if(deep > Deep) {
                value = root.val;
                Deep = deep;
            }
        }
        if(root.left != null) {
            findLeftValue(root.left, deep + 1);//deep + 1 包含回溯
        }
        if(root.right != null) {
            findLeftValue(root.right, deep + 1);
        }
    }
}

迭代

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int res = 0;
        if(root == null) return res;
        Queue que = new LinkedList<>();
        que.offer(root);
        while(!que.isEmpty()) {
            int size = que.size();
            for(int i = 0; i < size; i++) {
                TreeNode poll = que.poll();
                if(i == 0) res = poll.val;
                if(poll.left != null) que.offer(poll.left);
                if(poll.right != null) que.offer(poll.right);
            }
        } 
        return res;
    }
}

 路径总和  

题目链接/文章讲解/视频讲解:代码随想录

112. 路径总和

需要注意的是targetSum在递归里是减去结点值 比较其与0的大小

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) return false;
        //对目标值进行减操作 叶子结点直接比较目标值和0的关系
        targetSum -= root.val;
        if(root.left == null && root.right == null) {
            return targetSum == 0;
        }
        if(root.left != null) {
            if(hasPathSum(root.left, targetSum))
            return true;
        }
        if(root.right != null) {
            if(hasPathSum(root.right, targetSum)) {
                return true;
            }
        }
        return false;
    }
}

113. 路径总和ii 

class Solution {
    List> res = new ArrayList<>();
    List path = new LinkedList<>();
    public List> pathSum(TreeNode root, int targetSum) {
        if(root == null) return res;
        getPathSum(root, targetSum, res, path);
        return res;
    }
    public void getPathSum(TreeNode root, int targetSum, List> res, List path) {
        path.add(root.val);
        targetSum -= root.val;//减
        if(root.left == null && root.right == null && targetSum == 0) {
            res.add(new ArrayList<>(path));
        }
        if(root.left != null) {
            getPathSum(root.left, targetSum, res, path);
            path.remove(path.size() - 1);//回溯
        }
        if(root.right != null) {
            getPathSum(root.right, targetSum, res, path);
            path.remove(path.size() - 1);//回溯
        }
    }
}

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

题目链接/文章讲解/视频讲解:代码随想录

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

class Solution {
    Map map;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        map = new HashMap<>();
        for(int i = 0; i < inorder.length; i++) {
            map.put(inorder[i], i);
        }
        return getBulidTree(inorder, 0, inorder.length, postorder, 0, postorder.length);
    }
    public TreeNode getBulidTree(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
        if(inBegin >= inEnd || postBegin >= postEnd) {//不符合左闭右开
            return null;
        }
        int rootIndex = postEnd - 1;//后序根节点的索引
        int rootIndex1 = map.get(postorder[rootIndex]);//中序根节点索引
        TreeNode root = new TreeNode(inorder[rootIndex1]);//构造结点
        int lenOfLeft = rootIndex1 - inBegin;//左中序的长度
        root.left = getBulidTree(inorder, inBegin, rootIndex1, postorder, postBegin, postBegin + lenOfLeft);//左子树
        root.right = getBulidTree(inorder, rootIndex1 + 1, inEnd, postorder, postBegin + lenOfLeft, postEnd - 1);//右子树
        return root;
    }
}

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

class Solution {
    Map map;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        map = new HashMap<>();
        for(int i = 0; i < inorder.length; i++) {
            map.put(inorder[i], i);
        }
        return getBulidTree(preorder, 0, preorder.length, inorder, 0, inorder.length);
    }
    public TreeNode getBulidTree(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
        if(preBegin >= preEnd || inBegin >= inEnd) {
            return null;
        }
        int rootIndex = map.get(preorder[preBegin]);
        TreeNode root = new TreeNode(preorder[preBegin]);
        int lenOfLeft = rootIndex - inBegin;
        root.left = getBulidTree(preorder, preBegin + 1, preBegin + lenOfLeft + 1, inorder, inBegin, rootIndex);
        root.right = getBulidTree(preorder, preBegin + lenOfLeft + 1, preEnd, inorder, rootIndex + 1, inEnd);
        return root;
    }
}

你可能感兴趣的:(算法,leetcode,职场和发展)