当前位置:网站首页>[JZOF] 82 binary tree with a path of a certain value (1)
[JZOF] 82 binary tree with a path of a certain value (1)
2022-08-10 00:32:00 【sigh】
题目描述:
给定一个二叉树root和一个值 sum ,判断是否有从根节点到叶子节点的节点值之和等于 sum 的路径.
1.该题路径定义为从树的根结点开始往下一直到叶子结点所经过的结点.
2.叶子节点是指没有子节点的节点.
3.路径只能从父节点到子节点,不能从子节点到父节点.
4.总节点数目为n.
一、递归遍历:
import java.util.*;
public class Solution {
public boolean hasPathSum (TreeNode root, int sum) {
// write code here
if (root == null ) {
return false;
}
if (sum == root.val && root.left == null && root.right == null) {
return true;
}
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
二、层次遍历
import java.util.*;
/* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */
public class Solution {
/** * * @param root TreeNode类 * @param sum int整型 * @return bool布尔型 */
class Pair{
TreeNode node = null;
int curSum = 0;
// 内部类,Associate a partial path sum with the current node
public Pair(TreeNode node,int curSum){
this.node = node;
this.curSum = curSum;
}
}
public boolean hasPathSum (TreeNode root, int sum) {
// 根节点为空,返回false
if (root == null){
return false;
}
// Use a queue to store nodes during traversal
Queue<Pair> nodeQueue= new LinkedList<>();
Pair pair = new Pair(root,root.val);
nodeQueue.add(pair);
Pair curPair = null;
// 层次遍历,根,左,右
while(!nodeQueue.isEmpty()){
curPair = nodeQueue.poll();
// 左节点非空
if (curPair.node.left != null){
nodeQueue.add(new Pair(
curPair.node.left,
curPair.curSum+curPair.
node.left.val));
}
// 右节点非空
if (curPair.node.right != null){
nodeQueue.add(new Pair(
curPair.node.right,
curPair.curSum+curPair.
node.right.val));
}
// 判断是否为叶子节点,是则与sum进行比较
if (curPair.node.left==null&&curPair.node.right==null){
if (sum == curPair.curSum){
return true;
}
}
}
return false;
}
}
边栏推荐
- 全球不用交税的国家,为什么不交
- Janus Official DEMO Introduction
- Users should clearly know that quantitative trading is not a simple procedure
- 【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
- 如何知道电脑开机记录?
- Basic operations of xlrd and xlsxwriter
- 迁移学习 & 凯明初始化
- 杭电多校-Counting Stickmen-(思维+组合数+容斥)
- 干货!迈向鲁棒的测试时间适应
- Gartner全球集成系统市场数据追踪,超融合市场增速第一
猜你喜欢
matplotlib散点图颜色分组图例
Install win7 virtual machine in Vmware and related simple knowledge
迁移学习 & 凯明初始化
matplotlib散点图自定义坐标轴(文字坐标轴)
OSS文件上传
全面解析FPGA基础知识
【对象——对象及原型链上的属性——对象的操作方法】
【Burning】It's time to show your true strength!Understand the technical highlights of the 2022 Huawei Developer Competition in one article
伦敦银行情中短线的支撑和阻力位
信息系统项目管理师---第十一章项目风险管理历年考题
随机推荐
Forbidden (CSRF token missing or incorrect.): /
守护进程
华为云全流程护航《流浪方舟》破竹首发,打造口碑爆款
用户要清晰知道,量化交易并非简单的程序
(转)字符集编码标识符,数字表示字符编码
测试2年,当时身边一起入行的朋友已经月薪20k了,自己还没过万,到底差在了哪里?
用PLSQL导出Oracle一个表
&&、||、&、|
信息系统项目管理师---第十一章项目风险管理历年考题
量化交易接口系统有哪些稳定性?
【Leetcode】2104. Sum of Subarray Ranges
setter与getter访问器属性——数据驱动显示
2022-08-09 mysql/stonedb-慢SQL-Q16分析
Janus官方DEMO介绍
集合运算样例
新增一地公布2022下半年软考报考时间
[WeChat applet development (8)] Summary of audio background music playback problems
力扣:377. 组合总和 Ⅳ
web 面试高频考点 —— 性能优化篇(手写防抖、手写节流、XXS攻击、XSRF攻击)
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点