非常非常非常好!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 ...
随机推荐
- CSS3实现的渐变按钮,在IE7、IE6下的滤镜使用。
.ui-button { BORDER-LEFT-WIDTH:; ; COLOR: #fff; PADDING-BOTTOM: 0px; TEXT-ALIGN: center; PADDING-T ...
- 使用Visual Studio 2012 开发 Html5 应用
Visual Studio 一直以来是开发微软旗下应用的利器,只要是开发微软相关的应用无论是Windows程序,WPF,Asp.Net,WinRT Surface,WindowsPhone 等微软旗下 ...
- socket异步通信-如何设置成非阻塞模式、非阻塞模式下判断connect成功(失败)、判断recv/recvfrom成功(失败)、判断send/sendto
socket异步通信-如何设置成非阻塞模式.非阻塞模式下判断connect成功(失败).判断recv/recvfrom成功(失败).判断send/sendto 博客分类: Linux Socket s ...
- 常见的NoSql系统使用场景分析--转载
•Cassandra •特性:分布式与复制的权衡\根据列和键范围进行查询\BigTable类似的功能:列,列族\写比读快很多 •最佳适用:写操作较多,读比较少的时候.如果你的系统都是基于Java的时候 ...
- Java script 看看黑客怎么写的
在2011年的BlackHat DC 2011大会上Ryan Barnett给出了一段关于XSS的示例java script 代码: ($=[$=[ ] ] [(__=!$+$)[_=-~-~-~$] ...
- ELk 几篇好的文章
https://nxlog.co/docs/elasticsearch-kibana/using-nxlog-with-elasticsearch-and-kibana.html http://www ...
- UVA 11133 - Eigensequence DP
Given an increasing sequence of integers a1, a2, a3, . . . , ak, the E-transform produces a sequence ...
- Edius 安装 looks插件整理
需要安装looks插件到指定目录,在后面桥接时选择该目录. 包括安装looksbuilder插件.
- 15条规则解析JavaScript对象布局(__proto__、prototype、constructor)
大家都说JavaScript的属性多,记不过来,各种结构复杂不易了解.确实JS是一门入门快提高难的语言,但是也有其他办法可以辅助记忆.下面就来讨论一下JS的一大难点-对象布局,究竟设计JS这门语言的人 ...
- 凌乱的yyy
题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) 所以,他想知道他最多能参加 ...