Lintcode69 Binary Tree Level Order Traversal solution 题解
来源:互联网 发布:经典网络仙侠文 编辑:程序博客网 时间:2024/05/16 05:59
【题目描述】
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问)
【题目链接】
www.lintcode.com/en/problem/binary-tree-level-order-traversal/
【题目解析】
对于二叉树的问题,我们第一想到的就是DFS或者BFS, DFS更易于理解代码,如果处理数据量不是很大的话.对于这样的面试题,我建议用DFS来求解.
需要注意的点为:
对于一棵树,如果我们要求每一层的节点,并且存在一个二维数组里,首先我们要建一个二维数组,但是这个二维数组建多大的合适呢?我们就要求出这颗树的深度,根据深度来建立二维数组.
题目要求为从左往右添加,所以我们也就是要先放左边的节点,再放右边的节点.
对于这道题,我们首先就是要用DFS来求出这颗树的高度,之后再用DFS对于每一层遍历,这样节省了空间复杂度.
时间复杂度分析:由于两次DFS是并列的,并没有嵌套,所以我们的时间复杂度为O(n).
【参考答案】
www.jiuzhang.com/solutions/binary-tree-level-order-traversal/
阅读全文
0 0
- Lintcode69 Binary Tree Level Order Traversal solution 题解
- Lintcode69 Binary Tree Level Order Traversal solution 题解
- Lintcode70 Binary Tree Level Order Traversal II solution 题解
- Lintcode71 Binary Tree Zigzag Level Order Traversal solution 题解
- Lintcode70 Binary Tree Level Order Traversal II solution 题解
- Lintcode71 Binary Tree Zigzag Level Order Traversal solution 题解
- [LeetCode] Binary Tree Level Order Traversal Solution
- [题解][LeetCode][Binary Tree Level Order Traversal]
- LeetCode题解:Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal C++题解
- LeetCode题解:Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II java solution
- LeetCode题解:Binary Tree Level Order Traversal I and II
- LeetCode题解: Binary Tree Zigzag Level Order Traversal
- LeetCode 题解(32): Binary Tree Zigzag Level Order Traversal
- Leetcode题解(1):L102/Binary Tree Level Order Traversal
- LeetCode102 Binary Tree Level Order Traversal Java题解
- LeetCode题解:Binary Tree Level Order Traversal II
- parent 类
- 常用DB存储过程返回结果集的方法
- 淘淘商城系列——接收Queue消息
- JBPM(三)--- JPDL 流程定义语言
- Maven 项目生成或者update jdk变为1.5的问题
- Lintcode69 Binary Tree Level Order Traversal solution 题解
- 你见或者不见,我就在那里。不离不弃!--(shadow DOM)(前端冷知识)
- 网页抓取方式(二)--Jsoup
- centos vi 不显示颜色处理
- Lintcode70 Binary Tree Level Order Traversal II solution 题解
- 如何勾搭程序员?这有 11 个正确的姿势
- USACO-Section1.3 barn1[贪心]
- Binary String Matching
- containsKey