非常非常非常好!path-sum-iii
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的更多相关文章
- 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 ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 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 ...
- 437. Path Sum III
原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...
- 第34-3题:LeetCode437. Path Sum III
题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...
- 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 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- 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 ...
- LeetCode算法题-Path Sum III(Java实现)
这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...
- [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 ...
随机推荐
- mysql导出导入某张表
一般表数据少的话都用图形界面了,看着比较方便. 如果表中数据比较多,用图形界面极容易卡死,这个时候就要用到命令行了. 用命令行导出导入大量数据还是比较快的,方法如下: 导出库db1中的表table1: ...
- ZOJ3550 Big Keng(三分)
题意:给定一个立体的图形,上面是圆柱,下面是圆台,圆柱的底面半径和圆台的上半径相等,然后体积的V时,问这个图形的表面积最小可以是多少.(不算上表面).一开始拿到题以为可以YY出一个结果,就认为它是圆锥 ...
- iOS-xib(使用XIB自定义一个UIView )
1.新建一个xib视图
- eclipse创建python项目
http://jingyan.baidu.com/article/19192ad8173300e53f570757.html
- jquery常见问题
1.下面哪种说法是正确的? 您的回答:jQuery 是 JavaScript 库 2.jQuery 使用 CSS 选择器来选取元素? 您的回答:错误 正确答案:正确 3.jQuery 的简写是? 您的 ...
- volatile 关键字的复习
今天早上看何登成的微博(http://hedengcheng.com/?p=725) 对volatile 关键字语意进行了深入分析. 看完之后,用自己的话总结如下: 1.c/c++ volatile中 ...
- lintcode:线段树的修改
线段树的修改 对于一棵 最大线段树, 每个节点包含一个额外的 max 属性,用于存储该节点所代表区间的最大值. 设计一个 modify 的方法,接受三个参数 root. index 和 value.该 ...
- linux 如何让程序后台执行
$ (./test.sh &) $ setsid ./test.sh & $ nohup ./test.sh & 具体的转自:http://digdeeply.or ...
- Android笔记——Drawerlayout创建侧滑出菜单
1.首先务必导入support-v4包 2.布局文件主标签为<android.support.v4.widget.DrawerLayout>,并为其设置id 其子标签必须包 ...
- 关闭Centos写磁盘功能
一个Linux文件默认有3个时间.atime:对此文件的访问时间. ctime:此文件inode发生变化的时间. mtime:此文件的修改时间. 如果有多个小文件(比如Web服务器的页面上有多个小图片 ...