代码随想录算法训练营20期|第十八天|二叉树part05|513.找树左下角的值● 112. 路径总和 113.路径总和ii● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序

513.找树左下角的值

层序遍历YYDS,递归不是特别会

/**
 * 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 = 0;
        Queue queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                if (i == 0) {
                    res = cur.val;
                }
                if (cur.left != null) queue.add(cur.left);
                if (cur.right != null) queue.add(cur.right);
            }
        }
        return res;
    }
}

递归法:

/**
 * 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 {
    int maxDepth = Integer.MIN_VALUE;
    int res = 0;
    public int findBottomLeftValue(TreeNode root) {
        traversal(root, 0);
        return res;
    }

    private void traversal(TreeNode root, int depth){
        if (root.left == null && root.right == null) {
            if (depth > maxDepth) {
                maxDepth = depth;
                res = root.val;
            }
            return;
        }
        if (root.left != null) {
            depth++;
            traversal(root.left, depth);
            depth--;
        }
        if (root.right != null) {
            depth++;
            traversal(root.right, depth);
            depth--;
        }
        return;
    }
}

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;
        if (root.left == null && root.right == null) {
            return root.val == targetSum;
        }
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }
}

113.路径总和ii

/**
 * 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 List> pathSum(TreeNode root, int targetSum) {
        List> res = new ArrayList<>();
        if (root == null) return res;
        List path = new ArrayList<>();
        traversal(res, path, root, targetSum);
        return res;
    }

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

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

/**
 * 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[] inorder, int[] postorder) {
        return dfs(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1);
    }

    private TreeNode dfs(int[] inorder, int iLeft, int iRihgt, int[] postorder, int pLeft, int pRight) {
        if (iLeft > iRihgt || pLeft > pRight) return null;
        TreeNode cur = new TreeNode(postorder[pRight]);
        int i = 0;
        for (i = iLeft; i < inorder.length; i++) {
            if (inorder[i] == cur.val) break;
        }

        cur.left = dfs(inorder, iLeft, i - 1, postorder, pLeft, pLeft + i - iLeft - 1);
        cur.right = dfs(inorder, i + 1, iRihgt, postorder, pLeft + i - iLeft, pRight - 1);
        return cur;
    }
}

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) {
        Map map = new HashMap<>();
        for (int i = 0; i < inorder.length; i++) {
            map.put(inorder[i], i);
        }
        return buildTreeHelper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, map);
    }
    
    private TreeNode buildTreeHelper(int[] preorder, int pre_st, int pre_end, int[] inorder, int in_st, int in_end, Map map) {
        if (pre_st > pre_end || in_st > in_end) return null;
        
        TreeNode root = new TreeNode(preorder[pre_st]);
        int inRoot = map.get(root.val);
        int leftSize = inRoot - in_st;
        
        root.left = buildTreeHelper(preorder, pre_st + 1, pre_st + leftSize, inorder, in_st, inRoot - 1, map);
        root.right = buildTreeHelper(preorder, pre_st + leftSize + 1, pre_end, inorder, inRoot + 1, in_end, map);
        return root;
                    
    }
}

你可能感兴趣的:(代码随想录二刷,算法,数据结构)