写点什么

代码随想录 Day18 - 二叉树(五)

作者:jjn0703
  • 2023-07-16
    江苏
  • 本文字数:2748 字

    阅读完需:约 9 分钟

作业题

513. 找树左下角的值

package jjn.carl.binary_tree;
import commons.BinaryTreeHelper;import commons.TreeNode;
import java.util.ArrayList;import java.util.List;
/** * @author Jiang Jining * @since 2023-07-16 10:10 */public class LeetCode513 { private int maxDepth = -1; private int bottomLeftValue; public int findBottomLeftValue(TreeNode root) { traversal(root, 0); return bottomLeftValue; } private void traversal(TreeNode node, int depth) { if (node.left == null && node.right == null) { if (depth > maxDepth) { maxDepth = depth; bottomLeftValue = node.val; } return; } if (node.left != null) { traversal(node.left, depth + 1); } if (node.right != null) { traversal(node.right, depth + 1); } } public static void main(String[] args) { List<Integer> input = new ArrayList<>(List.of(1)); TreeNode node = BinaryTreeHelper.buildTree(input); int bottomLeftValue = new LeetCode513().findBottomLeftValue(node); System.out.println("bottomLeftValue = " + bottomLeftValue); }}
复制代码


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 boolean canReach;
public boolean hasPathSum(TreeNode root, int targetSum) { dfs(root, targetSum, 0); return canReach; }
private void dfs(TreeNode node, int targetSum, int currentSum) { if(node == null) { return; } if(node.left == null && node.right == null) { if(currentSum + node.val == targetSum) { canReach = true; } return; } if(node.left != null) { dfs(node.left, targetSum, currentSum + node.val); } if(node.right != null) { dfs(node.right, targetSum, currentSum + node.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<List<Integer>> pathSum(TreeNode root, int targetSum) {        List<List<Integer>> result = new ArrayList<>();        backtrack(result, root, new ArrayList<>(), targetSum, 0);        return result;    }
private void backtrack(List<List<Integer>> result, TreeNode root, List<Integer> path, int targetSum, int currentSum) { if(root == null) { return; } path.add(root.val); if(root.left == null && root.right == null) { if(currentSum + root.val == targetSum) { result.add(new ArrayList<>(path)); } return; } if(root.left != null) { backtrack(result, root.left, path, targetSum, currentSum + root.val); path.remove(path.size()- 1); } if(root.right != null) { backtrack(result, root.right, path, targetSum, currentSum + 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 {    Map<Integer, Integer> map;  // 方便根据数值查找位置    public TreeNode buildTree(int[] inorder, int[] postorder) {        map = new HashMap<>();        for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置            map.put(inorder[i], i);        }
return findNode(inorder, 0, inorder.length, postorder,0, postorder.length); // 前闭后开 }
public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) { // 参数里的范围都是前闭后开 if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树 return null; } int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置 TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点 int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数 root.left = findNode(inorder, inBegin, rootIndex, postorder, postBegin, postBegin + lenOfLeft); root.right = findNode(inorder, rootIndex + 1, inEnd, postorder, postBegin + lenOfLeft, postEnd - 1);
return root; }}
复制代码


发布于: 刚刚阅读数: 2
用户头像

jjn0703

关注

Java工程师/终身学习者 2018-03-26 加入

USTC硕士/健身健美爱好者/Java工程师.

评论

发布
暂无评论
代码随想录Day18 - 二叉树(五)_jjn0703_InfoQ写作社区