一、找树左下角的值

题目: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. CF1099F Cookies

    题目地址:CF1099F Cookies 树形dp套树形数据结构 对每个节点 \(i\) ,分两步进行: 1.令 \(f_i\) 为Mitya在节点 \(i\) 停止游戏最多可以吃到多少块饼干 我们可 ...

  2. 使用@JsonView注解控制返回的Json属性

    我也是刚看到原来还可以这么玩,但是我还是习惯使用Dto,我总感觉这样做的话实体类耦合程度有点高.还是记录以下,万一今后用到了呢 ⒈在实体类中使用接口来声明该实体类的多个视图. ⒉在实体类的属性get方 ...

  3. Templates<2>

    Part:template specialized Part1:template specialized #include <iostream> #include <stdio.h& ...

  4. 【转】Leveldb源码分析——1

    先来看看Leveldb的基本框架,几大关键组件,如图1-1所示. Leveldb是一种基于operation log的文件系统,是Log-Structured-Merge Tree的典型实现.LSM源 ...

  5. tomcat apr 部署

    背景 这还是为了高并发的事,网上说的天花乱坠的,加了apr怎么怎么好,我加了,扯淡.就是吹牛用.我还是认为,性能问题要考设计逻辑和代码解决,这些都是锦上添花的. 步骤 1 windows 部署简单,虽 ...

  6. js导出excel增加表头、mso-number-format定义数据格式

    问题1:增加表头 js导出表格时,只会导出table里的展现出的内容,如需增加表头等内容需要在页面获取的字符串中拼接表头的相关字符串,详细介绍如下: tableString:新增的表头内容字符串: c ...

  7. E: The package code needs to be reinstalled, but I can't find an archive for it.

    ubuntu安装软件时报错: E: The package code needs to be reinstalled, but I can't find an archive for it. 解决方法 ...

  8. 响应式页面-@media介绍

    01 响应式页面-@media介绍,   我们为什么要写自适应的页面(响应式页面) 众所周知,电脑.平板.手机的屏幕是差距很大的,假如在电脑上写好了一个页面,在电脑上看起来不错,但是如果放到手机上的话 ...

  9. pyhon 前面补充和set

    一, 主要内容. 补充一个字符串的基本操作 li = ["李嘉诚", "麻花藤", "黄海峰", "刘嘉玲"] s = ...

  10. 大数据python词频统计之hdfs分发-cacheArchive

    -cacheArchive也是从hdfs上进分发,但是分发文件是一个压缩包,压缩包内可能会包含多层目录多个文件 1.The_Man_of_Property.txt文件如下(将其上传至hdfs上) ha ...