Leetcode刷题第20天

时间:2022-08-29 15:21:56

一、找树左下角的值

题目:513. Find Bottom Left Tree Value

C++ Soution 1:

 /**
  * 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:
     int findBottomLeftValue(TreeNode* root)
     {

         queue<TreeNode*> q;
         q.push(root);
         TreeNode* curr;
         while (!q.empty())
         {
             curr = q.front();
             q.pop();
             if (curr->right != NULL) q.push(curr->right);
             if (curr->left != NULL) q.push(curr->left);
         }
         return curr->val;
     }
  };

C++ Soution 2:

 /**
  * 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:
     int findBottomLeftValue(TreeNode* root)
     {
         , res = root->val;
         helper(root, , max_depth, res);
         return res;
     }
     void helper(TreeNode* node, int depth, int& max_depth, int& res)
     {
         if (!node) return;
         if (depth > max_depth)
         {
             max_depth = depth;
             res = node->val;
         }
         helper(node->left, depth + , max_depth, res);
         helper(node->right, depth + , max_depth, res);
     }
 };

Leetcode刷题第20天

二、二叉树的层次遍历

题目:102. Binary Tree Level Order Traversal

C++ Soution 1:

 /**
  * 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:
     vector<vector<int>> levelOrder(TreeNode* root)
     {
         if (!root) return {};
         vector<vector<int>> res;
         queue<TreeNode*> q;
         q.push(root);
         while (!q.empty())
         {
             int count = q.size();
             vector<int> temp;
             while (count--)
             {
                 TreeNode* Qu = q.front();
                 q.pop();
                 if (Qu->left) q.push(Qu->left);
                 if (Qu->right) q.push(Qu->right);
                 temp.push_back(Qu->val);
             }
             res.push_back(temp);
         }
         return res;
     }
 };

三、最大二叉树

 /*
 struct TreeNode
 {
 int val;
 TreeNode *left;
 TreeNode *right;
 TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };
 */

 class Solution
 {
 public:
     TreeNode* constructMaximumBinaryTree(vector<int>& nums)
     {
         )
             return NULL;
         else
         {
             ;
             ; i < nums.size(); i++)
             {
                 if (maxNum < nums[i])
                 {
                     maxNum = nums[i];
                     index = i;
                 }
             }
             vector<int> left(nums.begin(), nums.begin() + index);
             vector<, nums.end());
             TreeNode* root = new TreeNode(maxNum);
             root->left = constructMaximumBinaryTree(left);
             root->right = constructMaximumBinaryTree(right);
             return root;
         }
     }
 };

四、相同的树

题目:100. Same Tree

C++ Soution 1:

 /**
  * 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 isSameTree(TreeNode* p, TreeNode* q)
     {
         if(!p && !q) return true;
         if(!p || !q) return false;
         if(p->val != q->val)
             return false;
         else
             return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
     }
 };

五、二叉树的垂直遍历

题目:987. Vertical Order Traversal of a Binary Tree

C++ Soution 1:

 /**
  * 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:
     vector<vector<int>> verticalTraversal(TreeNode* root)
     {
         map<int, vector<int> > m;
         queue<pair<int, TreeNode*> > q;
         q.push(make_pair(, root));
         while (!q.empty())
         {
             set<pair<int, int> > tmp;
             int len = q.size();
             ; i < len; ++i)
             {
                 auto p = q.front(); q.pop();
                 tmp.insert(make_pair(p.first, p.second->val));
                 , p.second->left));
                 , p.second->right));
             }

             for (auto p : tmp) m[p.first].push_back(p.second);
         }

         vector<vector<int> > res;
         for (auto kv : m) res.push_back(kv.second);
         return res;
     }
 };

Leetcode刷题第20天