@@ -309,25 +309,25 @@ public:
3093090112.路径总和
310310
311311``` java
312- class solution {
313- public boolean haspathsum ( treenode root , int targetsum ) {
312+ class Solution {
313+ public boolean hasPathSum ( TreeNode root , int targetSum ) {
314314 if (root == null ) {
315315 return false ;
316316 }
317- targetsum -= root. val;
317+ targetSum -= root. val;
318318 // 叶子结点
319319 if (root. left == null && root. right == null ) {
320- return targetsum == 0 ;
320+ return targetSum == 0 ;
321321 }
322322 if (root. left != null ) {
323- boolean left = haspathsum (root. left, targetsum );
324- if (left) { // 已经找到
323+ boolean left = hasPathSum (root. left, targetSum );
324+ if (left) { // 已经找到,提前返回
325325 return true ;
326326 }
327327 }
328328 if (root. right != null ) {
329- boolean right = haspathsum (root. right, targetsum );
330- if (right) { // 已经找到
329+ boolean right = hasPathSum (root. right, targetSum );
330+ if (right) { // 已经找到,提前返回
331331 return true ;
332332 }
333333 }
@@ -336,39 +336,39 @@ class solution {
336336}
337337
338338// lc112 简洁方法
339- class solution {
340- public boolean haspathsum ( treenode root , int targetsum ) {
339+ class Solution {
340+ public boolean hasPathSum ( TreeNode root , int targetSum ) {
341341
342342 if (root == null ) return false ; // 为空退出
343343
344344 // 叶子节点判断是否符合
345- if (root. left == null && root. right == null ) return root. val == targetsum ;
345+ if (root. left == null && root. right == null ) return root. val == targetSum ;
346346
347347 // 求两侧分支的路径和
348- return haspathsum (root. left, targetsum - root. val) || haspathsum (root. right, targetsum - root. val);
348+ return hasPathSum (root. left, targetSum - root. val) || hasPathSum (root. right, targetSum - root. val);
349349 }
350350}
351351```
352352
353353迭代
354354
355355``` java
356- class solution {
357- public boolean haspathsum ( treenode root , int targetsum ) {
356+ class Solution {
357+ public boolean hasPathSum ( TreeNode root , int targetSum ) {
358358 if (root == null ) return false ;
359- stack < treenode > stack1 = new stack <>();
360- stack < integer > stack2 = new stack <>();
359+ Stack< TreeNode > stack1 = new Stack <> ();
360+ Stack< Integer > stack2 = new Stack <> ();
361361 stack1. push(root);
362362 stack2. push(root. val);
363- while (! stack1. isempty ()) {
363+ while (! stack1. isEmpty ()) {
364364 int size = stack1. size();
365365
366366 for (int i = 0 ; i < size; i++ ) {
367- treenode node = stack1. pop();
367+ TreeNode node = stack1. pop();
368368 int sum = stack2. pop();
369369
370370 // 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
371- if (node. left == null && node. right == null && sum == targetsum ) {
371+ if (node. left == null && node. right == null && sum == targetSum ) {
372372 return true ;
373373 }
374374 // 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
@@ -387,8 +387,9 @@ class solution {
387387 }
388388}
389389```
390- ``` Java 統一迭代法
391- public boolean hasPathSum(TreeNode root, int targetSum) {
390+ ``` Java
391+ class Solution {
392+ public boolean hasPathSum (TreeNode root , int targetSum ) {
392393 Stack<TreeNode > treeNodeStack = new Stack<> ();
393394 Stack<Integer > sumStack = new Stack<> ();
394395
@@ -422,38 +423,39 @@ class solution {
422423 }
423424 return false ;
424425 }
426+ }
425427```
426428
4274290113.路径总和-ii
428430
429431``` java
430- class solution {
431- public List<List<Integer > > pathsum (TreeNode root , int targetsum ) {
432+ class Solution {
433+ public List<List<Integer > > pathSum (TreeNode root , int targetSum ) {
432434 List<List<Integer > > res = new ArrayList<> ();
433435 if (root == null ) return res; // 非空判断
434436
435437 List<Integer > path = new LinkedList<> ();
436- preorderdfs (root, targetsum , res, path);
438+ preOrderDfs (root, targetSum , res, path);
437439 return res;
438440 }
439441
440- public void preorderdfs (TreeNode root , int targetsum , List<List<Integer > > res , List<Integer > path ) {
442+ public void preOrderDfs (TreeNode root , int targetSum , List<List<Integer > > res , List<Integer > path ) {
441443 path. add(root. val);
442444 // 遇到了叶子节点
443445 if (root. left == null && root. right == null ) {
444446 // 找到了和为 targetsum 的路径
445- if (targetsum - root. val == 0 ) {
447+ if (targetSum - root. val == 0 ) {
446448 res. add(new ArrayList<> (path));
447449 }
448450 return ; // 如果和不为 targetsum,返回
449451 }
450452
451453 if (root. left != null ) {
452- preorderdfs (root. left, targetsum - root. val, res, path);
454+ preOrderDfs (root. left, targetSum - root. val, res, path);
453455 path. remove(path. size() - 1 ); // 回溯
454456 }
455457 if (root. right != null ) {
456- preorderdfs (root. right, targetsum - root. val, res, path);
458+ preOrderDfs (root. right, targetSum - root. val, res, path);
457459 path. remove(path. size() - 1 ); // 回溯
458460 }
459461 }
@@ -1626,3 +1628,4 @@ public class Solution {
16261628<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
16271629 <img src =" ../pics/网站星球宣传海报.jpg " width =" 1000 " />
16281630</a >
1631+
0 commit comments