lc513找二叉树左下角的值
文章&视频:lc513
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode* root, int depth) {
if (root->left==NULL && root->right==NULL) {
if (depth > maxDepth) {
maxDepth = depth;
result = root->val;
}
return;
}
if (root->left) {
depth++;
traversal(root->left, depth);
depth--;
}
if (root->right) {
depth++;
traversal(root->right, depth);
depth--;
}
return;
}
int findBottomLeftValue (TreeNode* root) {
int depth = 0;
traversal(root, depth);
return result;
}
};
本文介绍了一个名为Solution的类,用于解决LC513题目的二叉树问题,通过递归方法计算并返回二叉树左下角的最大值。核心是`traversal`和`findBottomLeftValue`函数。
401

被折叠的 条评论
为什么被折叠?



