Leetcode: Binary Tree Level Order Transversal

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]
]

难度:78。其实思路很清楚,就是把树看成一个有向图,进行BFS。BST的通常做法都是维护一个队列。这道题的难度在于,如何在队列不断的入队出队操作中,确定哪些node是一个层次的。想了很久没什么好办法,参考了网上的做法,发现他在维护两个数:一个是父节点所在层次的节点在当前队列中的数目ParentNumInQ,另一个是子节点所在层次的节点在当前队列中的数目ChildNumInQ。初始数值为1和0. 每次父节点出队,ParentNumInQ--,每次子节点入队,ParentNumInQ++。一旦ParentNumInQ减至0,说明当前父节点层次已全部出队,全部被存入LinkedList中,这就得到了一层的所有节点。接下来需要一些更新,ParentNumInQ = ChildNumInQ, ChildNumInQ = 0开始考虑下一层。

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
12         ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>> ();
13         if (root == null) return lists;
14         LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
15         queue.add(root);
16         int ParentNumInQ = 1;
17         int ChildNumInQ = 0;
18         ArrayList<Integer> list = new ArrayList<Integer>();
19         while (!queue.isEmpty()) {
20             TreeNode cur = queue.poll();
21             list.add(cur.val);
22             ParentNumInQ--;
23             if (cur.left != null) {
24                 queue.add(cur.left);
25                 ChildNumInQ++;
26             }
27             if (cur.right != null) {
28                 queue.add(cur.right);
29                 ChildNumInQ++;
30             }
31             if (ParentNumInQ == 0) {
32                 ParentNumInQ = ChildNumInQ;
33                 ChildNumInQ = 0;
34                 lists.add(list);
35                 list = new ArrayList<Integer>();
36             }
37         }
38         return lists;
39     }
40 }
时间: 2024-10-05 01:57:30

Leetcode: Binary Tree Level Order Transversal的相关文章

Leetcode: Binary Tree Level Order Transversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

[leetcode]Binary Tree Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历的实现. 解题思路:二叉树的层序遍历可以用bfs或者dfs来实现.这里使用的dfs实现,代码比较简洁.实际上,二叉树的先序遍历就是dfs实现.   比如一棵树如下: 1 /  \ 2       3 /    \    /   \ 4     5  6    7    二叉树的先序遍历为{1,2,4,5,3,6,7},可以看到这个遍

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary

LeetCode——Binary Tree Level Order Traversal

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] ] 原题链接:

LeetCode——Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

LeetCode: Binary Tree Level Order Traversal II [107]

[题目] Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

[LeetCode]Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal II Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 re

LeetCode: Binary Tree Level Order Traversal 解题报告

Binary Tree Level Order Traversal 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   7return its leve

leetcode Binary Tree Level Order Traversal I II

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] ] /** *