https://leetcode.com/problems/path-sum-iii/

最终我还是没做出好的解法。还是看的别人的解法。

即使看了别人的解法,开始还实现错了。

还有很长的路要走。

package com.company;

// https://discuss.leetcode.com/topic/64388/simple-ac-java-solution-dfs
// 比我的方法,好多了。唉。
// 我开始准备DFS,然后在栈里面遍历的,非常复杂。
// 唉,还是思维太局限了。
// 我的第一种解法,还实现错了。直接递归是造成和跳跃。。唉。。差劲 class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
} class Solution { public int pathSum(TreeNode root, int sum) {
if (root == null) {
return 0;
} int ret = pathWithRoot(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
return ret;
}
private int pathWithRoot(TreeNode root, int sum) {
if (root == null) {
return 0;
}
int ret = 0;
if (root.val == sum) {
ret++;
}
ret += pathWithRoot(root.left, sum-root.val) + pathWithRoot(root.right, sum-root.val);
return ret;
}
} public class Main { public static void main(String[] args) {
// write your code here
System.out.println("Hello");
Solution solution = new Solution(); TreeNode root = new TreeNode(10);
TreeNode root1 = new TreeNode(5);
TreeNode root2 = new TreeNode(-3);
TreeNode root3 = new TreeNode(3);
TreeNode root4 = new TreeNode(2);
TreeNode root5 = new TreeNode(11);
TreeNode root6 = new TreeNode(3);
TreeNode root7 = new TreeNode(-2);
TreeNode root8 = new TreeNode(1);
root.left = root1;
root.right = root2;
root1.left = root3;
root1.right = root4;
root2.right = root5;
root3.left = root6;
root3.right = root7;
root4.right = root8; int ret = solution.pathSum(root, 8);
System.out.printf("Result is %d\n", ret); }
}

下面是写错了的代码。。

package com.company;

// https://discuss.leetcode.com/topic/64388/simple-ac-java-solution-dfs
// 比我的方法,好多了。唉。
// 我开始准备DFS,然后在栈里面遍历的,非常复杂。
// 唉,还是思维太局限了。
// 我的第一种解法,还实现错了。直接递归是造成和跳跃。。唉。。差劲 class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
} class Solution { public int pathSum(TreeNode root, int sum) {
if (root == null) {
return 0;
}
int ret = 0;
if (root.val == sum) {
ret++;
}
ret += pathSum(root.left, sum)
+ pathSum(root.left, sum-root.val)
+ pathSum(root.right, sum)
+ pathSum(root.right, sum-root.val);
System.out.printf("root %d, ret %d, sum %d\n", root.val, ret, sum);
return ret;
}
} public class Main { public static void main(String[] args) {
// write your code here
System.out.println("Hello");
Solution solution = new Solution(); TreeNode root = new TreeNode(10);
TreeNode root1 = new TreeNode(5);
TreeNode root2 = new TreeNode(-3);
TreeNode root3 = new TreeNode(3);
TreeNode root4 = new TreeNode(2);
TreeNode root5 = new TreeNode(11);
TreeNode root6 = new TreeNode(3);
TreeNode root7 = new TreeNode(-2);
TreeNode root8 = new TreeNode(1);
root.left = root1;
root.right = root2;
root1.left = root3;
root1.right = root4;
root2.right = root5;
root3.left = root6;
root3.right = root7;
root4.right = root8; int ret = solution.pathSum(root, 8);
System.out.printf("Result is %d\n", ret); }
}

非常非常非常好!path-sum-iii的更多相关文章

  1. 47. leetcode 437. Path Sum III

    437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...

  2. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  3. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  4. 437. Path Sum III

    原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...

  5. 第34-3题:LeetCode437. Path Sum III

    题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...

  6. LeetCode_437. Path Sum III

    437. Path Sum III Easy You are given a binary tree in which each node contains an integer value. Fin ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. LeetCode 437. Path Sum III (路径之和之三)

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. LeetCode算法题-Path Sum III(Java实现)

    这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...

  10. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. KIP-32 Add timestamps to Kafka message

    通过KIP32,Kafka的每条消息都加进了时间戳,这个KIP在0.10.0.0被加入. 说到“时间”,先贴张图,娱乐一下(如果对星球大战系列电影不熟的话,请自动略过……) 这个KIP的文档在 KIP ...

  2. Long和Date数据类型之间相互转换代码

    static final SimpleDateFormat DATETIME_SEC_STR = new SimpleDateFormat("yyyyMMddHHmmss"); 1 ...

  3. POC

    大概就是原型验证的意思 验证概念 编辑 概念验证(Proof of concept,简称POC)是对某些想法的一个不完整的实现,以证明其可行性,示范其原理,其目的是为了验证一些概念或理论.在计算机安全 ...

  4. XHProf的安装和使用(PHP性能测试神器)

    XHProf是Facebook开发的性能调试工具,帮助我们的PHP程序性能调优,更加健壮.XHProf安装和使用方法将在本章讲解.XHProf是PHP的PECL扩展.没有XDeBug那些耗费资源,更加 ...

  5. POJ1811 Prime Test(miller素数判断&&pollar_rho大数分解)

    http://blog.csdn.net/shiyuankongbu/article/details/9202373 发现自己原来的那份模板是有问题的,而且竟然找不出是哪里的问题,所以就用了上面的链接 ...

  6. ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)

    ZOJ 2971 Give Me the Number 题目 ZOJ 2311 Inglish-Number Translator 题目 //两者题目差不多,细节有点点不一样,因为不是一起做的,所以处 ...

  7. NetCore第一步:千里之行 始于环境构筑

    今年的6月28号,微软发布了一个正式版本 NetCore.发布的同时,也同时发布了CoreStudio. 这个激动人心的时刻,让跨平台已经不再是什么神话. 让我们一起来开始Core的开发之旅吧. 万事 ...

  8. DevExpress GridControl 复合表头/表头分层设计.

    首先创建一个窗体,将GridControl控件拖到窗体中. 然后 Click here to change view  -> Convert to ->  BandedGridView   ...

  9. (2)jni编程学习笔记

    先说说NDK和jni的关系吧,这两个看起来挺容易搞混的 我到网上也查了一些资料: java的jni提供了一个调用c语言函数的接口,其实就是一个java函数,这个函数没有任何内容,这个函数调用时直接进入 ...

  10. js小技巧(二)

    //移动的图层,拖动 1.<span style='position:absolute;width:200;height:200;background:red' onmousedown=Mous ...