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

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. hdu3577 Fast Arrangement

    Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...

  2. centos 7下设置.net core项目开机自启动

    1.在etc/systemd/system下创建xxx.service文件 例如:vi /etc/systemd/system/ubif.service2.编辑 ubif.service内容如下: [ ...

  3. 通过js正则表达式实例学习正则表达式基本语法

    正则表达式又叫规则表达式,一般用来检查字符串中是否有与规则相匹配的子串,达到可以对匹配的子串进行提取.删除.替换等操作的目的.先了解有哪些方法可以使用正则对字符串来实现这些操作: RegExpObje ...

  4. LEETCODE - 160【相交链表】

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  5. LINUX - pthread_mutex_lock

    原文链接:https://www.cnblogs.com/fengbohello/p/7571722.html 互斥的概念 在多线程编程中,引入了对象互斥锁的概念,来保证共享数据操作的完整性. 每个对 ...

  6. spring再学习之AOP准备

    一.aop思想: 横向重复,纵向抽取 1.乱码 2.事务管理 3,action 二.spring能够为容器中管理的对象生成代理对象 1.spring能帮我们生成代理对象 2.spring实现aop的原 ...

  7. windows 命令行 cmd 控制exe程序输入输出并比较

    参考 https://www.cnblogs.com/zccz14/p/4588634.html 例子: 对exe输入输出 使用fc比较不同

  8. Caffe入门:对于抽象概念的图解分析

    Caffe的几个重要文件 用了这么久Caffe都没好好写过一篇新手入门的博客,最近应实验室小师妹要求,打算写一篇简单.快熟入门的科普文. 利用Caffe进行深度神经网络训练第一步需要搞懂几个重要文件: ...

  9. vue 如何重绘父组件,当子组件的宽度变化时候

    vue 如何重绘父组件,当子组件的宽度变化时候 vue & dynamic el-popover position demo https://codepen.io/xgqfrms/pen/wv ...

  10. free open music API all in one

    free open music API all in one music API SoundCloud xgqfrms · free https://w.soundcloud.com/player/? ...