二叉树层次遍历

http://www.lintcode.com/zh-cn/problem/binary-tree-level-order-traversal/#

错误点:queue是抽象的,实例化时,实例成LinkedList

    q.isEmpyt()  不是q.empty()

    入队 q.offer(E)    出队  q.poll()

 1 public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
 2         ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
 3         if(root == null) return result;
 4         Queue<TreeNode> q = new LinkedList<TreeNode>();
 5         q.offer(root);
 6         ArrayList<Integer> arr = null;
 7         while(!q.isEmpty()){
 8             arr = new ArrayList<Integer>();
 9             int num = q.size();
10             for(int i = 0; i <num; i++) {
11                 TreeNode temp = q.poll();
12                 arr.add(temp.val);
13                 if(temp.left != null) q.offer(temp.left);
14                 if(temp.right != null) q.offer(temp.right);
15             }
16             result.add(arr);
17         }
18         return result;
19     }

时间: 2024-08-27 11:55:56

二叉树层次遍历的相关文章

【数据结构】二叉树层次遍历

package 蓝桥练习; public class 二叉树层次遍历 { public static int MAXSIZE = 100; public static Node queue[] = new Node[MAXSIZE]; public static void main(String[] args) { Node h = new Node('H', null, null); Node i = new Node('I', null, null); Node f = new Node('

java实现二叉树层次遍历

public class BSTNode<T extends Comparable<T>> { T key; // 关键字(键值) BSTNode<T> left; // 左孩子 BSTNode<T> right; // 右孩子 BSTNode<T> parent; // 父结点 public BSTNode(T key, BSTNode<T> parent, BSTNode<T> left, BSTNode<T&g

毕业了C++二叉树层次遍历

//代码经过测试,赋值粘贴即可用#include<iostream> #include<stdio.h> #include<stack> #include<queue> #include<malloc.h> using namespace std; //二叉树结点 typedef struct BTNode{ char data; struct BTNode *lchild; struct BTNode *rchild; }BTNode; //模

毕业了-java二叉树层次遍历算法

/*************************************** * 时间:2017年6月23日 * author:lcy * 内容:二叉树的层次遍历 * 需要借助队列这个数据结构,直接import就可以了 * 1.首先将根节点放入队列中. 2.当队列为非空时,循环执行步骤3到步骤5,否则执行6: 3.出队列取得一个结点,访问该结点: 4.若该结点的左子树为非空,则将该结点的左子树入队列: 5.若该结点的右子树为非空,则将该结点的右子树入队列: 6.结束. ***********

1040. 二叉树层次遍历

Description 给出一棵二叉树,求它的层次遍历结果. [二叉树的遍历问题是一种精神,务必领会] Input Format 第一行,N<1000000,表示二叉树节点数. 默认序号为0的节点为树根.接下来共N-1行,依次表示序号为1,...,N-1的节点的父亲节点序号. 如果一个节点有两个孩子节点,左孩子节点序号总是小于右孩子节点序号. Output Format 仅一行,二叉树的层次遍历结果.节点序号间用空格隔开. Hint Sample Input 6 0 1 1 0 4 Sample

二叉树层次遍历(剑指Offer面试题32:从上到下打印二叉树)

图1所示为二叉树的层次遍历,即按照箭头所指方向,按照1.2.3的层次顺序,对二叉树每个节点进行访问 (此图反映的是自左至右的层次遍历,自右至左的方式类似). 要进行层次遍历,需要建立一个队列.先将二叉树头节点入队列,然后出队列,访问该节点, 如果它有左子树,则将左子树的根结点入队:如果它有右子树,则将右子树的根结点入队.然后出队列,对出队节点访问, 如此反复直到队列为空为止. 1 import java.util.*; 2 class TreeNode 3 { 4 int val; 5 Tree

编程之美——二叉树层次遍历

方法一:从根节点开始,将每层节点压入一个数组,cur代表当前访问节点,last代表下一层第一个节点,遍历数组可得层次遍历: 代码: 1 #include<iostream> 2 #include<queue> 3 #include<vector> 4 using namespace std; 5 6 template<class T> 7 class binaryTree 8 { 9 struct node 10 { 11 T elem; 12 node *

二叉树层次遍历_判断结点所属层次

#include<stdlib.h> #include<stdio.h> #include<stack> #define N 50 using namespace std; typedef struct tree{ char ch; struct tree *lchild; struct tree *rchild; }BitTree; //数组输入 BitTree *CreateTree(int A[], int i, int n){ BitTree *bt; if(i

毕业了-二叉树层次遍历算法-借助循环队列这个数据结构来实现,悟:数据结构是用来实现算法的

//代码进过测试,直接可以拿来用//关键就是那一点未透--队列.// 关键就是一个出队,一个入队操作.#include<iostream> #include<stdio.h> #include<stack> #include<queue> #include<malloc.h> # define MaxSize 100 using namespace std; //二叉树结点 typedef struct BTNode{ char data; st

第六章例题二叉树层次遍历

1.指针实现 #include <iostream> #include <vector> #include <queue> #include <cstdio> #include <cstring> using namespace std; #define maxn 100 struct Node { bool have_value; int value; /*节点结构体*/ Node *left,*right; Node():have_value