leetcode刷题 | 关于二叉树的题型总结2

简介: 关于二叉树的题型总结2

leetcode刷题 | 关于二叉树的题型总结2

题目链接

129. 求根节点到叶节点数字之和 - 力扣(LeetCode)

437. 路径总和 III - 力扣(LeetCode)

124. 二叉树中的最大路径和 - 力扣(LeetCode)

求根节点到叶节点数字之和

三种解法

第一种带有返回值的dfs,返回值为某一路径的值

classSolution {

   

   publicintsumNumbers(TreeNoderoot) {

       returndfs(root,0);

   }

   publicintdfs(TreeNoderoot,intsum){

       // sum存储的是每一条路径的和

       if(root==null) return0;

       sum=sum*10+root.val;

       if(root.left==null&&root.right==null) returnsum;//返会这条路径的值

       elsereturndfs(root.left,sum)+dfs(root.right,sum); //将左右节点的路径加和

   }

}

第二种不带返回值的递归+回溯

classSolution {

   intsum=0,num=0;

   publicintsumNumbers(TreeNoderoot) {

       dfs(root);

       returnsum;

   }

   publicvoiddfs(TreeNoderoot){

       if(root==null) return;

       num=num*10+root.val;//一直没有到叶子路径

       if(root.left==null&&root.right==null)

           sum+=num; //叶子节点,将这一路径的值加和

       dfs(root.left);

       dfs(root.right);

       num= (num-root.val)/10;

       

   }

}

第三种广度优先搜索,定义两个栈,一个存储节点一个存储节点值

classSolution {

   publicintsumNumbers(TreeNoderoot) {

       Deque<TreeNode>deq1=newArrayDeque();

       Deque<Integer>deq2=newArrayDeque();

       deq1.add(root);

       deq2.add(root.val);

       intsum=0;

       while(!deq1.isEmpty()){

           TreeNodenode=deq1.poll();

           intnum=deq2.poll();

           if(node.left==null&&node.right==null){

               sum+=num;

           }

           if(node.left!=null){

               deq1.add(node.left);

               deq2.add(num*10+node.left.val);

           }

           if(node.right!=null){

               deq1.add(node.right);

               deq2.add(num*10+node.right.val);

           }

       }

       returnsum;

   }

}

路径总和 III

classSolution {

   

   publicintpathSum(TreeNoderoot, longtargetSum) {

       if(root==null) return0;

       returndfs(root,targetSum)+pathSum(root.left,targetSum)+pathSum(root.right,targetSum);

   }

   publicintdfs(TreeNoderoot, longtargetSum){

       if(root==null) return0;

       intres=0;

       if(root.val==targetSum)

           res++;

       returnres+dfs(root.left,targetSum-root.val)+dfs(root.right,targetSum-root.val);

   }

}

classSolution {

   privateintres;

   publicintpathSum(TreeNoderoot, inttargetSum) {      

       if(root==null) returnres;

       help(root,targetSum);

       pathSum(root.left,targetSum);

       pathSum(root.right,targetSum);

       returnres;

   }

   publicvoidhelp(TreeNoderoot,inttarget){

       if(root==null) return;

       if(root.val==target) res++;

       help(root.left,target-root.val);

       help(root.right,target-root.val);

   }

}

前缀和解法

classSolution {

   publicintpathSum(TreeNoderoot, longtargetSum) {

       Map<Long, Integer>prefix=newHashMap<Long, Integer>();

       prefix.put(0L, 1);

       returndfs(root, prefix, 0, targetSum);

   }

   publicintdfs(TreeNoderoot,Map<Long,Integer>prefix,longcur,longtarget){

       if (root==null) return0;

       intres=0;

       cur+=root.val; //当前节点的前缀和

       res=prefix.getOrDefault(cur-target,0);

       prefix.put(cur,prefix.getOrDefault(cur,0)+1);

       res+=dfs(root.left,prefix,cur,target);

       res+=dfs(root.right,prefix,cur,target);

       //回溯

       prefix.put(cur,prefix.getOrDefault(cur,0)-1);

       returnres;

 

   }

}

二叉树中的最大路径和

遍历顺序为后序遍历,左右中,先把左右节点的最大值获取到,将当前节点作为拐点,连接左右两个节点,更新路径最大值后,返会当前节点+Math.max(左节点,右节点),也就是返会当前节点获得的最大值

classSolution {

   intmax=Integer.MIN_VALUE;

   publicintmaxPathSum(TreeNoderoot) {

       dfs(root);

       returnmax;

   }

   publicintdfs(TreeNoderoot){

       if (root==null) return0;

       intLeftMax=Math.max(dfs(root.left),0);//返会当前节点右节点的最大值

       intRightMax=Math.max(dfs(root.right),0);//返回当前节点左节点的最大值

       max=Math.max(max,root.val+LeftMax+RightMax);//更新路径的最大值

       returnroot.val+Math.max(LeftMax,RightMax);//返回当前节点的最大值  

   }

}


相关文章
|
1月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
63 2
|
26天前
|
存储 算法
二叉树进阶-学会层序遍历助你一次刷完leetcode10道题
文章深入探讨了二叉树的层序遍历方法,并展示了如何通过队列实现层序遍历的算法逻辑,同时指出掌握层序遍历技巧可以帮助解决LeetCode上的多道相关题目。
二叉树进阶-学会层序遍历助你一次刷完leetcode10道题
|
26天前
|
算法 Java
LeetCode第94题二叉树的中序遍历
文章介绍了LeetCode第94题"二叉树的中序遍历"的解法,使用递归实现了中序遍历的过程,遵循了"左根右"的遍历顺序,并提供了清晰的Java代码实现。
LeetCode第94题二叉树的中序遍历
|
1月前
|
Python
【Leetcode刷题Python】50. Pow(x, n)
本文介绍了LeetCode第50题"Pow(x, n)"的解法,题目要求实现计算x的n次幂的函数,文章提供了递归分治法的详细解析和Python实现代码。
14 1
|
1月前
|
算法 Python
【Leetcode刷题Python】73. 矩阵置零
本文介绍了LeetCode第73题的解法,题目要求在给定矩阵中将所有值为0的元素所在的行和列全部置为0,并提供了一种原地算法的Python实现。
16 0
【Leetcode刷题Python】73. 矩阵置零
|
1月前
|
存储 算法 Java
LeetCode经典算法题:二叉树遍历(递归遍历+迭代遍历+层序遍历)以及线索二叉树java详解
LeetCode经典算法题:二叉树遍历(递归遍历+迭代遍历+层序遍历)以及线索二叉树java详解
55 0
|
1月前
|
算法 Java
LeetCode初级算法题:子数组最大平均数+二叉树的最小深度+最长连续递增序列+柠檬水找零
LeetCode初级算法题:子数组最大平均数+二叉树的最小深度+最长连续递增序列+柠檬水找零
30 0
|
1月前
|
Python
【Leetcode刷题Python】1467. 两个盒子中球的颜色数相同的概率
本文介绍了LeetCode第50题"Pow(x, n)"的解法,题目要求实现计算x的n次幂的函数,文章提供了递归分治法的详细解析和Python实现代码。
16 0
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
38 6
|
1月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
33 4