本文主要是介绍代码随想录|day 17(待补全),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Day 17
一、理论学习
513. 找树左下角的值 - 力扣(LeetCode)
误看成了最左下,劈里啪啦写的
/*** 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:void traversal(TreeNode* root,vector<int>&res){if(root==nullptr) return;traversal(root->left,res);if(!res.empty()) return;res.push_back(root->val);}int findBottomLeftValue(TreeNode* root) {vector<int>res;traversal(root,res);return res[0];}
};
二、刷题部分
513. 找树左下角的值 - 力扣(LeetCode)
深搜
/*** 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:void traversal(TreeNode* root,int height,int &curval,int &curheight){if(root==nullptr) return;height++;traversal(root->left,height,curval,curheight);traversal(root->right,height,curval,curheight);if(height>curheight){curheight=height;curval=root->val;}}int findBottomLeftValue(TreeNode* root) {int curval,curheight=0;traversal(root,0,curval,curheight);return curval;}
};
这篇关于代码随想录|day 17(待补全)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!