leetcode 112 Path Sum ----- java
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
给定一棵树和一个数,判断这个数是不是从根节点到叶子节点的和。
递归实现很简单。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public boolean hasPathSum(TreeNode root, int sum) { if( root == null)
return false;
if( root.left == null && root.right == null)
return sum==root.val; return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val); }
}
leetcode 112 Path Sum ----- java的更多相关文章
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 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 ...
- [LeetCode] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- [LeetCode] 112. Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Java for LeetCode 112 Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Java [Leetcode 112]Path Sum
题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- LeetCode 112. Path Sum (二叉树路径之和)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [Leetcode]112. Path Sum -David_Lin
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- 工具&符号
持续更新中...... 1.RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RP ...
- AOP面向切面编程
1.AOP Aop(aspect object programming)面向切面编程 功能: 让关注点代码与业务代码分离! 关注点 重复代码就叫做关注点: 切面 关注点形成的类,就叫切面(类)! 面向 ...
- jQuery对表单元素的取值和赋值操作代码
使用常规的思路:$(“#keyword”).value 取值是取不到的,因为此时$(‘#keydord’)已经不是个element,而是个jquery对象,所以应该使用:$(“#keyword”).v ...
- MBProgressHUD.h file not found
MBProgressHUD框架,怎么我导入MBProgressHUD+MJ.h会报错.(即MBProgressHUD+MJ根本不存在),我看其他人的视屏又可以导入 MBProgressHUD.h fi ...
- sourcetree使用问题汇总
1.可优先查阅博文<git 用户手册 1.5.3及后续版本使用>: 2.问题1 Cloning into 'folder'... warning: templates not found ...
- 标签navtab
创建navtab 创建一个navtab有以下两种方式: 1.Data属性:DOM添加属性data-toggle="navtab"后,单击触发. a链接示例: <a href= ...
- bjui给出的一个标准应用的首页
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...
- Ubuntu 14.10 下MySQL无法远程连接问题
安装好MySQL之后,如果需要远程连接,那么需要做一些配置,否则会出现一些类似的错误,如 mysql root用户ERROR (): mysql 远程登录 ERROR () mysql 远程登录200 ...
- Oracle排序问题
关于oracle排序的几点认识: (1)oracle本身不具有任何默认排序功能,要想排序,必须使用order by,而order by后的数据行默认是asc(升序排列),要降序选择desc : (2) ...
- LeetCode----3 Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...