LeetCode -- Binary Tree Level Order Traversal
2015-10-05 10:27
183 查看
Question:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree
return its level order traversal as:
Analysis:
问题描述:给出一棵二叉树,返回它的节点的层次遍历。
思路:二叉树的层次遍历要用到队列。每次访问一个节点时,该节点出队列,同时它的左右节点分别入队。但是这个题目要求我们将每个层次上的节点分开,因此要额外使用一个指针指向下一层的初始节点(即本层节点在下层初始节点开始前结束)。
1)遍历每一层的节点时,终止条件就是当前节点是否等于下一层的起始节点。
2)整个循环的终止是判断队列是否为空了。
Answer:
思路二:在网上看到另外一个网友的解答。没有使用队列,而是用了两个链表,每次第一个链表代表本层的所有节点,第二个链表记录本层节点的所有孩子。这样,依次将值放入最终结果集即可。
Answer:
Conclusion:
每次做树的题目总是很混乱,不论是递归还是用队列或者栈。要多多练习这方面的题目。
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree
{3,9,20,#,#,15,7},
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
Analysis:
问题描述:给出一棵二叉树,返回它的节点的层次遍历。
思路:二叉树的层次遍历要用到队列。每次访问一个节点时,该节点出队列,同时它的左右节点分别入队。但是这个题目要求我们将每个层次上的节点分开,因此要额外使用一个指针指向下一层的初始节点(即本层节点在下层初始节点开始前结束)。
1)遍历每一层的节点时,终止条件就是当前节点是否等于下一层的起始节点。
2)整个循环的终止是判断队列是否为空了。
Answer:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<List<Integer>>(); List<Integer> l = new ArrayList<Integer>(); if(root == null) return res; if(root.left == null && root.right == null) { l.add(root.val); res.add(l); return res; } TreeNode start; //记录了每一层的开始 Queue<TreeNode> node = new LinkedList<TreeNode>(); node.offer(root); while(!node.isEmpty()) { TreeNode cur = node.peek(); List<Integer> thisLevel = new ArrayList<Integer>(); start = null; while(node.peek() != start && !node.isEmpty()) { cur = node.poll(); if(start == null) { if(cur.left != null) start = cur.left; else if(cur.right != null) start = cur.right; } thisLevel.add(cur.val); if(cur.left != null) node.offer(cur.left); if(cur.right != null) node.offer(cur.right); } res.add(thisLevel); } return res; } }
思路二:在网上看到另外一个网友的解答。没有使用队列,而是用了两个链表,每次第一个链表代表本层的所有节点,第二个链表记录本层节点的所有孩子。这样,依次将值放入最终结果集即可。
Answer:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<List<Integer>>(); if(root == null) return res; if(root.left == null && root.right == null) { List<Integer> list = new ArrayList<Integer>(); list.add(root.val); res.add(list); return res; } List<TreeNode> l1 = new ArrayList<TreeNode>(); List<TreeNode> l2 = new ArrayList<TreeNode>(); l1.add(root); while(l1.size() != 0) { List<Integer> val = new ArrayList<Integer>(); for(int i=0; i<l1.size(); i++) { TreeNode cur = l1.get(i); val.add(cur.val); if(cur.left != null) l2.add(cur.left); if(cur.right != null) l2.add(cur.right); } res.add(val); List<TreeNode> temp = l1; l1 = l2; l2 = temp; l2.clear(); } return res; } }
Conclusion:
每次做树的题目总是很混乱,不论是递归还是用队列或者栈。要多多练习这方面的题目。
相关文章推荐
- Bossies 2015: The Best of Open Source Software Awards
- 用doxygen+graphviz自动化生成代码文档(附详细教程)
- Java集合类详解
- Python面向对象编程 - 一个记事本程序范例(一)
- ActiveReports 报告应用程序教程 (2)---上市类报告
- hdu5496 Beauty of Sequence
- VMWare下虚拟机NAT共享方式上网的配置说明
- 解决交叉编译mysql出现缺少/usr/local/ncurses/lib/libncurses.a
- delphi 资源文件管理 Resources and Images
- 二维数组对比一维数组的取值差别是多了一个行偏移的计算
- <div>之定位
- 大并发高负载下的PHP-FPM参数调优
- Java 最佳实践
- 逆向for语句
- php版快速排序
- 【BeiJing wc2012】【BZOJ2661】连连看
- c访问mysql的完整例子
- 指针的逆向
- 《剑指offer》 相应 在线测试地址
- output子句用法 output inserted.autoId