leetcode题解-1302-层数最深叶子节点的和 发表于 2019-12-30 题目 My way dfs带层数状态遍历 123456789101112131415161718192021222324252627282930313233343536373839/** * 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 {private: int sum = 0; int maxLevel = -1; void dfs(TreeNode* root, int level) { if (!root) return; if (maxLevel < level) { maxLevel = level; sum = root->val; } else if (maxLevel == level) { sum += root->val; } dfs(root->left, level+1); dfs(root->right, level+1); }public: int deepestLeavesSum(TreeNode* root) { dfs(root, 0); return sum; }}; sum, maxLevel也可以改成dfs函数的引用参数。