代码随想录第十八天 | 二叉树终章

 今天是本周二叉树的最后一章了,内容都是偏难的

513.找树左下角的值

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            root = q.poll();
            if(root.right!=null){
                q.offer(root.right);

            }
            if(root.left != null){
                q.offer(root.left);
            }
        }
        return root.val;
    }
}

用Queue或者别的collection也都可以,就是dfs一直往下探,直到遇到叶子。

112. 路径总和

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null){
            return false;
            
        }
        if(root.left == null && root.right == null){
            return targetSum == root.val;
        }
        return hasPathSum(root.left, targetSum- root.val)||hasPathSum(root.right, targetSum - root.val);
    }
}

遍历每一条路径,去看有没有总和与targetSum相同的路径

 

113.路径总和ii

class Solution {
    List<List<Integer>> result = new ArrayList<>();
        
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if (root == null) return result;
        List<Integer> list = new ArrayList<>();
        list.add(root.val);
        search(root, list, root.val, targetSum);

        return result;
    }
    public void search(TreeNode root, List<Integer> path, int sum, int target){
        if(root.left == null && root.right == null){
            if(sum == target){
                result.add(new ArrayList<>(path));
            }
            return;
        }
        if(root.left != null){
            path.add(root.left.val);
            search(root.left, path, sum + root.left.val, target);
            path.remove(path.size() - 1);
        }
        
        if(root.right != null){
            path.add(root.right.val);
            search(root.right, path, sum + root.right.val, target);
            path.remove(path.size() - 1);
        }
    }
}

要记录满足条件的每一条路径

 

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

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        int i_len = inorder.length;
        int p_len = postorder.length;
        if(i_len == 0 || p_len == 0){
            return null;
        }
        //通过后序序列,查找子树的根节点
        int root_val = postorder[p_len - 1];
        //构造根节点
        TreeNode root = new TreeNode(root_val);
        //遍历中序序列,确定根结点在中序序列中的位置,从而确定左右子树
        int k = 0;
        for (int i = 0; i < i_len; i++) {
            if(root_val == inorder[i]){
                k = i;
                break;
            }
        }
        //分割左右子树,分别创建左右子树的中序、后序序列
        int[] left_in = Arrays.copyOfRange(inorder, 0, k);
        int[] left_post = Arrays.copyOfRange(postorder, 0, k);
        root.left = buildTree(left_in,left_post);
        
        int[] right_in = Arrays.copyOfRange(inorder, k + 1, i_len);
        int[] right_post = Arrays.copyOfRange(postorder, k, p_len - 1);
        root.right = buildTree(right_in,right_post);
        return root;
    }
}

自己写的太乱了,直接用的大佬的原码

 

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

class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        LinkedList<List<Integer>> result = new LinkedList<>();
        if (root == null)
            return result;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            List<Integer> oneLevel = new ArrayList<>();
            // 每次都取出一层的所有数据
            int count = queue.size();
            for (int i = 0; i < count; i++) {
                TreeNode node = queue.poll();
                oneLevel.add(node.val);
                if (node.left != null)
                    queue.add(node.left);
                if (node.right != null)
                    queue.add(node.right);
            }
            // 每次都往队头塞
            result.addFirst(oneLevel);
        }
        return result;
    }
}

一样很乱,直接用原码

 

今天的题很难很多,二叉树这块二刷时一定要重点复习

posted @ 2022-10-30 11:50  小猫Soda  阅读(24)  评论(0)    收藏  举报