一、找树左下角的值

题目: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);
     }
 };

二、二叉树的层次遍历

题目: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天的更多相关文章

  1. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  2. LeetCode刷题总结-树篇(上)

          引子:刷题的过程可能是枯燥的,但程序员们的日常确不乏趣味.分享一则LeetCode上名为<打家劫舍 |||>题目的评论: 如有兴趣可以从此题为起点,去LeetCode开启刷题之 ...

  3. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配   17.8% 困难 45 跳跃游戏 II   25.5% 困难 55 跳跃游戏   30.6% 中等 122 买卖股票的最佳时机 II C ...

  4. C#LeetCode刷题-栈

    栈篇 # 题名 刷题 通过率 难度 20 有效的括号 C#LeetCode刷题之#20-有效的括号(Valid Parentheses) 33.0% 简单 42 接雨水   35.6% 困难 71 简 ...

  5. C#LeetCode刷题-动态规划

    动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串   22.4% 中等 10 正则表达式匹配   18.8% 困难 32 最长有效括号   23.3% 困难 44 通配符匹配   17.7% ...

  6. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.6% 中等 5 最长回文子串   22.4% 中等 6 Z字形变换   35.8% 中等 8 字符串转整数 (atoi)   ...

  7. C#LeetCode刷题-双指针

    双指针篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.5% 中等 11 盛最多水的容器   43.5% 中等 15 三数之和   16.1% 中等 16 最接近的三数之和   3 ...

  8. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  9. C#LeetCode刷题-数学

    数学篇 # 题名 刷题 通过率 难度 2 两数相加   29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 ...

随机推荐

  1. SQL Server - 索引详细教程 (聚集索引,非聚集索引)

    转载自:https://www.cnblogs.com/hyd1213126/p/5828937.html 作者:爱不绝迹 (一)必读:深入浅出理解索引结构 实际上,您可以把索引理解为一种特殊的目录. ...

  2. 从无文件技术到使用隐写术:检查Powload的演变

    来源:https://blog.trendmicro.com/trendlabs-security-intelligence/from-fileless-techniques-to-using-ste ...

  3. Mongoose简介

    Mongoose 官网地址:http://mongoosejs.com/   ,Mongoose 为node.js提供了优雅的,针对mongodb的ODM(Object Document Mappin ...

  4. Perf -- Linux下的系统性能调优工具,第 1 部分【转】

    转自:https://www.ibm.com/developerworks/cn/linux/l-cn-perf1/ Perf 简介 Perf 是用来进行软件性能分析的工具. 通过它,应用程序可以利用 ...

  5. VC显示网页验证码、模拟CSDN网页登录

    摘要:by:koma 这两天,本来想花点时间研究一下QQ空间.农场外挂,于是抓包分析一了下,只可惜,在QQ网页登录时进行了加密处理,可惜我对网页编程一窍不通.有些朋友曾讲过那些是通过JS代码进行加密, ...

  6. FTP服务器基础设定

    1.安装vsftpd文件服务器 sudo apt-get install vsftpd 2.配置文件:/etc/vsftpd/vsftpd.conf 严格来说,整个 vsftpd 的配置文件就只有这个 ...

  7. mingw zlib-1.2.11 win32-static.patch

    diff --git a/Makefile.in b/Makefile.in index 1852192..c88f5b5 100644 --- a/Makefile.in +++ b/Makefil ...

  8. 026_lsof命令经验总结

    一.lsof处理删除文件未释放句柄问题. 但是如果你不知道是哪个文件,或者是很多文件都有这样的情况,那你需要使用如下命令 lsof |grep deleted 注:这个deleted表示该已经删除了的 ...

  9. python操作mysql数据库增删改查的dbutils实例

    python操作mysql数据库增删改查的dbutils实例 # 数据库配置文件 # cat gconf.py #encoding=utf-8 import json # json里面的字典不能用单引 ...

  10. MSChart的研究(转)

    介绍MSChart的常用属性和事件 MSChart的元素组成 最常用的属性包括 ChartAreas:增加多个绘图区域,每个绘图区域包含独立的图表组.数据源,用于多个图表类型在一个绘图区不兼容时. A ...