Posts Tagged 【bfs】Binary Tree Level Order Traversal I && II

Binary Tree Level Order Traversal

  Total Accepted: 47754  Total Submissions: 161177 My Submissions

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

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

/*
【BFS通常框架】
通常使用队列实现FIFO
    初始化队列Q
    Q={起点s},s标记为已经访问
    while(Q不为空) {
        取队首head,head出队
        if(head处达到目标) {...}
        所有head未访问的相邻节点加入队列
        标记head已经被访问
    }
eg:
int[] queue = new int[n];
int head = 0;tail = 0;
queue[tail++] = root;
while(head < tail) {
   //do thing 
}
【三种思路,最后一种最为简洁】
===【每个节点记录自己所在的层数】
一个list记录遍历的先后结构,一个list记录遍历的节点的层数
如果输出遍历路线,一般还要保持父节点的遍历中存的位置
最后将两个list的结果对照来输出
===【用一个符号来比较层的转移】
用一个变量节点来记录下一层的开始节点
while(!Q.isEmpty()) {
    Node n = Q.peek();
    Node n2depth = null'
    while(n != n2depth && !Q.isEmpty()) {
        Q.remove();
        if(n2depth == null) {
            n2depth = n not null 子节点
        }
        Q.add(n not null 子节); n = Q.peek();
    }
    //do thing for this layer
}
===【用两个队列来存上下两层的数据】
从root开始,放到第一个队列
接着取出第一队列,将其相邻或子节点放到下一个队列
如此反复
*/
/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 
 /*
Input:	{}
Output:	null
Expected:[]
 */
public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<List<Integer>>();
        if(root == null) return lists;
        List<Integer> list; //注意每次都要new
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while(!queue.isEmpty()) {
            Queue<TreeNode> temp = new LinkedList<TreeNode>();
            list = new ArrayList<Integer>();
            while(!queue.isEmpty()) {
                TreeNode n = queue.remove();
                list.add(n.val);
                if(n.left != null) temp.add(n.left);
                if(n.right != null) temp.add(n.right);
            }
            lists.add(list);
            queue = temp;
        }
        return lists;
    }
}



Binary Tree Level Order Traversal II

  Total Accepted: 38751  Total Submissions: 125221 My Submissions

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 traversal as:

[
  [15,7],
  [9,20],
  [3]
]

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
 
 /*没啥好搞的
 Colletcions.reverse(lists);
 */
public class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<List<Integer>>();
        if(root == null) return lists;
        List<Integer> list; //注意每次都要new
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.add(root);
        while(!queue.isEmpty()) {
            Queue<TreeNode> temp = new LinkedList<TreeNode>();
            list = new ArrayList<Integer>();
            while(!queue.isEmpty()) {
                TreeNode n = queue.remove();
                list.add(n.val);
                if(n.left != null) temp.add(n.left);
                if(n.right != null) temp.add(n.right);
            }
            lists.add(list);
            queue = temp;
        }
        Collections.reverse(lists);
        return lists;
    }
}



Have you met this question in a real interview?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值