代码随想录算法day16 | 二叉树 part04 | 513.找树左下角的值,112. 路径总和

513.找树左下角的值

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;
//         // System.out.println(targetSum);
//         int result=sum(root,targetSum);
//         // System.out.println(result);
//         if(result==-100) return true;
//         else return false;


//     }
//     public int sum(TreeNode root,int target){
//         int tmp=target-root.val;
//         System.out.println("root: " + tmp);
//         if(root.left==null&&root.right==null) return tmp;

//         if(root.left!=null){
//             tmp=sum(root.left,tmp); 
//             System.out.println("tmp: " + tmp);
//             if(tmp!=0) tmp+=root.left.val;
//             else {return tmp;}
//         }
        
//         // System.out.println(tmp);
//         if(root.right!=null){
//             tmp=sum(root.right,tmp);
            
//             // System.out.println(tmp);
//             if(tmp!=0) tmp+=root.right.val;
//             else {return tmp;}
//         }
        
//         return tmp;
//     }
// }
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        // 如果树为空,则返回false
        if (root == null) {
            return false;
        }
        
        // 检查根节点的路径和
        int result = sum(root, targetSum);
        
        // 如果结果为0,说明存在一条路径,其和为targetSum
        return result == 0;
    }

    public int sum(TreeNode root, int target) {
        int tmp = target - root.val;
        System.out.println("Current Node Value: " + root.val + ", Remaining Target: " + tmp);
        
        // 如果是叶子节点,返回剩余的目标和
        if (root.left == null && root.right == null) {
            return tmp;
        }
        
        // 记录左子树的结果
        int leftResult = -1; // -1表示无效路径
        if (root.left != null) {
            leftResult = sum(root.left, tmp);
            // 如果找到了有效的路径和为0,直接返回
            if (leftResult == 0) {
                return 0;
            }
        }
        
        // 记录右子树的结果
        int rightResult = -1; // -1表示无效路径
        if (root.right != null) {
            rightResult = sum(root.right, tmp);
            // 如果找到了有效的路径和为0,直接返回
            if (rightResult == 0) {
                return 0;
            }
        }
        
        // 返回有效的路径,如果左子树没有路径,返回右子树结果,反之亦然
        // 如果两个子树均无有效路径,则返回非零数值
        return (leftResult == 0 || rightResult == 0) ? 0 : -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.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;
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值