非常非常非常好!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 ...
随机推荐
- 最小PE文件讨论
1.实例1国外的人写的最小的PE文件--97Bytes 4D5A0000504500004C0101006A2A58C30000000000000000040003010B01080004000000 ...
- HDOJ 1062 Text Reverse
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Pytho中两种方式导入模块的差别
1.使用import module,只是把模块导入,访问模块中的函数名或者是属性是必须使用点运算符(.)来访问,否则直接访问会提示找不到这些函数或者属性. 2.使用from numpy import ...
- javascript和swf在网页中交互的一些总结
Javascript和swf在网页中交互一般可有以下几种情况: 1.swf和这些调用的javascript在同域 2.swf和这些调用的javascript在不同域,比如加载远程的swf然后call别 ...
- 白话CSS3的新特性
声明:这篇文章不是手册,所以不会说的很详细,只是告诉初学者CSS3显著的改进有啥,高手老手绕行. 一.在边框上的改进 1.可以给方框加圆角了,值越大越圆,解决了过去大方框的不美观 2.可以给控件加阴影 ...
- Android的Testing和Instrumentation
Android提供了一系列强大的测试工具,它针对Android的环境,扩展了业内标准的JUnit测试框架.尽管你可以使用JUnit测试Android工程,但Android工具允许你为应用程序的各个方面 ...
- 李洪强iOS经典面试题12
1.说说响应链 答: 事件响应链.包括点击事件,画面刷新事件等.在视图栈内从上至下,或者从下之上传播. 可以说点事件的分发,传递以及处理.具体可以去看下touch事件这块.因为问的太抽象化了 严重怀 ...
- angularJS之$watch、$digest和$apply方法
最近项目上使用了比较多的angular JS,一直都对它感觉比较陌生,总觉得有点反直觉,这段时间,准备下定决心弄明白,这个框架到底是怎么一回事,以及它的工作原理,生命周期……一点一点的啃完它吧.首先, ...
- RTMP/RTP/RTSP/RTCP的区别
RTCP RTMP/RTP/RTSP/RTCP的区别 http://blog.csdn.net/frankiewang008/article/details/7665547 流媒体协议介绍(rtp/r ...
- linux下使用crontab定时备份MYSQL数据库的方法:
摘要 linux下使用crontab定时备份MYSQL数据库的方法: 只需按照下面3步做,一切都在你的掌控之下: 第一步:在服务器上配置备份目录代码: ------------------------ ...