特定深度节点链表-求解每一层二叉树从左到右遍历形成的链表

list-of-depth-lcci

  • 这是关于二叉树的问题,遍历每一层的结点并且存在链表中。
  • 可以采取队列类似于广度优先搜索的方法进行搜索。每次出队列时,首先记录队列中还有多少个同一层的结点,再遍历所有这些结点并且将左右结点再次进栈。
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
vector<ListNode*> listOfDepth(TreeNode* tree) {
vector<ListNode*> list;
queue<TreeNode*> que;
TreeNode* node=NULL;
que.push(tree);
while(!que.empty()){
ListNode* head=new ListNode(-1);
ListNode* temp=head;
int ans=que.size();
for(int i=0;i<ans;i++){
node=que.front();
temp->next=new ListNode(node->val);
que.pop();
temp=temp->next;
if(node->left){
que.push(node->left);
}
if(node->right){
que.push(node->right);
}
}
list.push_back(head->next);
}
return list;
}
};
//输入:[1,2,3,4,5,null,7,8]
// 1
// / \
// 2 3
// / \ \
// 4 5 7
// /
// 8
int main(){
TreeNode* t1=new TreeNode(1);
TreeNode* t2=new TreeNode(2);
TreeNode* t3=new TreeNode(3);
TreeNode* t4=new TreeNode(4);
TreeNode* t5=new TreeNode(5);
TreeNode* t7=new TreeNode(7);
TreeNode* t8=new TreeNode(8);
t4->left=t8;
t2->left=t4;t2->right=t5;
t3->right=t7;
t1->left=t2;t1->right=t3;
Solution solution;
vector<ListNode*> vec=solution.listOfDepth(t1);
for(int i=0;i<vec.size();i++){
ListNode* list=vec[i];
while(list){
cout<<list->val<<" ";
list=list->next;
}
cout<<endl;
}
system("pause");
return 0;
}

LeetCode-[list-of-depth-lcci]的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  3. LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  4. LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  5. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  6. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

  7. 【LeetCode】Maximum Depth of Binary Tree

    http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...

  8. [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  9. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  10. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

随机推荐

  1. c语言实现--双向循环链表操作

    1,双向链表相当于两个单向循环链表. 2,双向链表的结点定义. 1 struct DULNode 2 { 3 int data; 4 struct DULNode * prior; 5 struct ...

  2. Nacos学习与实战

    1. 什么是Nacos 官网:https://nacos.io/zh-cn/index.html Nacos是阿里巴巴集团开源的项目,Nacos 致力于帮助您发现.配置和管理微服务. Nacos提供了 ...

  3. 02、Jmeter正则表达式提取器

    转载自:http://blog.csdn.net/quiet_girl/article/details/50724313 在使用Jmeter过程中,会经常使用到正则表达式提取器提取器,虽然并不直接涉及 ...

  4. GO - go mod使用原理

    Go Module 依赖管理 go mod使用 原理及使用ref: https://xuanwo.io/2019/05/27/go-modules/ go module的稳定路径: https://l ...

  5. 51nod1459 带权最短路

    1459 迷宫游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分 ...

  6. Chrome Canary crashed bug

    Chrome Canary crashed bug Aw, Snap https://support.google.com/chrome/?p=e_awsnap clear cache, 使用隐身模式 ...

  7. Google reCAPTCHA 2 : Protect your site from spam and abuse & Google reCAPTCHA 2官方教程

    1

  8. 如何使用 js 检测控制台被用户打开了

    如何使用 js 检测控制台被用户打开了 js solutions 监听 F12 事件 监听键盘快捷键组合 Ctrl + Shift + I Option + Command + I Object.to ...

  9. Jamstack Conf 2020

    Jamstack Conf 2020 Jamstack Conf Virtual https://jamstackconf.com/virtual/ Conf Schedule https://jam ...

  10. DNS & TXT recode & Google Search Console

    DNS & TXT recode & Google Search Console domain name verification / 域名验证 demo DNS TXT recode ...