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;
}
}