112. Path Sum
来源:互联网 发布:sqlserver删除主键 编辑:程序博客网 时间:2024/06/18 18:20
果然还是递归比较好理解,首先判断是否有根节点、根节点的值是否正好等于sum;然后递归调用hasPathSum,注意第二个参数改为sum-root->val:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool hasPathSum(TreeNode* root, int sum) { if (root == NULL) return false; if (root->val == sum && !(root->left) && !(root->right)) return true; return (hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val)); }};
阅读全文
0 0
- 112. Path Sum && 113. Path Sum II
- 112. Path Sum&113. Path Sum II
- 112.Path Sum
- LeetCode 112. Path Sum
- [LeetCode]112.Path Sum
- 【LeetCode】112.Path Sum
- [Leetcode] 112. Path Sum
- [leetcode] 112.Path Sum
- 112.Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum LeetCode
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 112. Path Sum
- 说说&和&&的区别
- js封装运动属性
- js的replace()传入函数时的用法
- 关于VGG以及一些零碎
- Java作业(第二周)
- 112. Path Sum
- eclipse安装maven插件
- 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区) C. Coconut
- 修改tomcat端口后,在eclispe中启动tomcat,竟然还是原来的端口!
- Tensorboard: No graph definition files were found
- 使用jquery-confirm优化JS弹出框
- 反汇编分析之:继承,多重继承,多层继承,父子类指针本质
- ImportError: No module named 'tensorflow.tensorboard.tensorboard'
- tagVariant