一、找树左下角的值

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

    opengl里面的深度缓存  在现实生活中,一个实心物体挡在另外一个实心物体的前面, 后面的那个物体有部分会被遮盖掉 那么opengl里面如何模拟这个情况呢? 每个物体的每个像素都有一个深度缓存的值( ...

  2. k64 datasheet学习笔记45---10/100-Mbps Ethernet MAC(ENET)之概述

    1.前言 k64 ENET CORE 实现了10M/100Mbps的Ethernet MAC,与IEEE802.3-2002标准兼容. MAC层与全双工/半双工的10M/100Mbps以太网兼容: M ...

  3. vue2+axios在不同的环境打包不同的接口地址

    node.js的环境变量 process process 对象是一个 global (全局变量),提供有关信息,控制当前 Node.js 进程.作为一个对象,它对于 Node.js 应用程序始终是可用 ...

  4. boost 文件系统

    第 9 章 文件系统 目录 9.1 概述 9.2 路径 9.3 文件与目录 9.4 文件流 9.5 练习  该书采用 Creative Commons License 授权 9.1. 概述 库 Boo ...

  5. 题解-POI2009 WSP-Island

    Problem bzoj1137 题意概要:给定一个凸多边形坐标.点按顺时针编号 \(1\) 到 \(n\).任意两点之间都有一条长度为欧氏距离的边相连.边相交处可以自由穿行.有 \(m\) 条边不能 ...

  6. 迁移学习(Transfer Learning)

    原文地址:http://blog.csdn.net/miscclp/article/details/6339456 在传统的机器学习的框架下,学习的任务就是在给定充分训练数据的基础上来学习一个分类模型 ...

  7. 微信小程序-动态设置背景色navigationBarBackgroundColor的值

    查看API: wx.setNavigationBarColor(OBJECT) 代码: wx.setNavigationBarColor({ frontColor: '#ffffff', // 必写项 ...

  8. $Django 路飞学城项目简介

    - 基于极验实现动态验证码 - 在线视频播放:cc,HTML用的Flash - 基于Rest Framework实现 API接口 - 自定义rest认证token 认证 - 序列化以及自定义验证对请求 ...

  9. python操作三大主流数据库(10)python操作mongodb数据库④mongodb新闻项目实战

    python操作mongodb数据库④mongodb新闻项目实战 参考文档:http://flask-mongoengine.readthedocs.io/en/latest/ 目录: [root@n ...

  10. ifconfig和ping

    命令: ifconfig 对应英文: configure a network interface 作用: 查看 / 配置计算机当前的网卡配置信息 安装: sudo apt install net-to ...