一、找树左下角的值

题目: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. 【转】python3解析库lxml

    转自:http://www.cnblogs.com/zhangxinqi/p/9210211.html 阅读目录 1.python库lxml的安装 2.XPath常用规则 (1)读取文本解析节点 (2 ...

  2. Java 处理 XML

    DOM 优缺点:实现 W3C 标准,有多种编程语言支持这种解析方式,并且这种方法本身操作上简单快捷,十分易于初学者掌握.其处理方式是将 XML 整个作为类似树结构的方式读入内存中以便操作及解析,因此支 ...

  3. Freemaker:操作集合

    <#if (id?index_of('Base') >= 0)> <choose> <when test="rootOrgID !=null and ro ...

  4. oracle 11.2.0.4 rac 打补丁

    本次安装pus环境是11.2.0.4 rac,打的patch为11.2.0.4.180717 (Includes Database PSU),gi补丁和数据库补丁一起打 安装最新opatch版本 un ...

  5. SpringBoot和SpringCloud面试题

    一. 什么是springboot 1.用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 2.创建独立的spring引用程序 main方法运行 ...

  6. 30个mysql千万级大数据SQL查询优化技巧详解

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  7. Boostrap轮图片可以左右滑动

    记得引用Boostrap的js和css html代码: <div id="Mycarousel" class="carousel slide col-md-12&q ...

  8. OracleAES加密

    OracleAES加密 (2012-04-29 21:52:15)转载▼标签: oracle aes 加密 it 分类: 开发-- 加密函数CREATE OR REPLACE FUNCTION FUN ...

  9. 虚拟机时间同步14 Aug 04:09:18 ntpdate[2941]: no server suitable for synchronization found

    因为虚拟机经常挂起,所以需要时间同步 [root@slave1 /root]$ cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime cp: over ...

  10. Android性能优化:手把手带你全面实现内存优化

      前言 在 Android开发中,性能优化策略十分重要 本文主要讲解性能优化中的内存优化,希望你们会喜欢 目录   1. 定义 优化处理 应用程序的内存使用.空间占用 2. 作用 避免因不正确使用内 ...