leetcode 404. Sum of Left Leaves DFS深度优先遍历
来源:互联网 发布:奥飞数据 广州 编辑:程序博客网 时间:2024/05/17 23:16
Find the sum of all left leaves in a given binary tree.
Example:
3
/ \
9 20
/ \
15 7
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
题意很简单,就是求所有的左叶子节点的value的和,
代码如下:
#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <bitset>using namespace std;/*struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};*/class Solution {public: int sumOfLeftLeaves(TreeNode* root) { if (root == NULL) return 0; else if (root->left != NULL && root->left->left == NULL && root->left->right == NULL) return root->left->val + sumOfLeftLeaves(root->right); else return sumOfLeftLeaves(root->left) + sumOfLeftLeaves(root->right); }};
阅读全文
0 0
- leetcode 404. Sum of Left Leaves DFS深度优先遍历
- LeetCode 404 Sum of Left Leaves (DFS)
- LeetCode oj 404. Sum of Left Leaves (DFS||BFS)
- LeetCode笔记:404. Sum of Left Leaves
- 【leetcode】404. Sum of Left Leaves【E】
- LeetCode—404. Sum of Left Leaves
- [LeetCode]404. Sum of Left Leaves
- Leetcode 404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves
- 404. Sum of Left Leaves - leetcode
- [LeetCode]--404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves
- [leetcode]404. Sum of Left Leaves
- LeetCode-404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves
- LeetCode 404. Sum of Left Leaves
- leetcode 404. Sum of Left Leaves
- leetcode(88).404. Sum of Left Leaves
- 中国所有汉字-简体
- 初识MQTT协议(2)
- kali 2017开启ftp
- 年底了,我们都应该这么跟老板谈加薪
- Qt Creator的配置和开发初步测试
- leetcode 404. Sum of Left Leaves DFS深度优先遍历
- 瞬间高并发的处理方案
- Pointers and Arrays
- maven profile自动切换环境参数的两种实现方式
- Linux C Strtok函数
- RAM和ROM
- 一分钟告诉你究竟DevOps是什么鬼?
- 集合 ( Subset )
- JAVAGUI-计算器程序