码迷,mamicode.com
首页 > 其他好文 > 详细

lintcode69- Binary Tree Level Order Traversal- easy

时间:2017-10-22 11:11:41      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:==   ntc   www   traversal   node   ret   str   you   algo   

Given a binary tree, return the level order traversal of its nodes‘ values. (ie, from left to right, level by level).

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

Challenge 1: Using only 1 queue to implement it.

Challenge 2: Use DFS algorithm to do it.

 

1. BFS:和queue组合做,while(q.notEmpty)每层,嵌套for(层里每个)做。每层的任务就是取出此层节点,加入层的list,并且把孩子们推进queue。

2.DFS:一层一层的list造起来。每次规定加maxLevel这层的节点。DFS到这一层的时候加节点;null或者超的时候不加;还没到这层就继续向下走。

 

1. BFS:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */


public class Solution {
    /*
     * @param root: A Tree
     * @return: Level order a list of lists of integer
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
        // write your code here
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        
        if (root == null) {
            return result;
        }
        
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        queue.offer(root);
        
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> crtLevel = new ArrayList<Integer>();
            for (int i = 0; i < size; i++) {
                TreeNode node= queue.poll();
                crtLevel.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            result.add(crtLevel);
        }
        
        return result;
    }
}

 

2.DFS

// version 2:  DFS
public class Solution {
    /**
     * @param root: The root of binary tree.
     * @return: Level order a list of lists of integer
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> results = new ArrayList<List<Integer>>();
        
        if (root == null) {
            return results;
        }
        
        int maxLevel = 0;
        while (true) {
            List<Integer> level = new ArrayList<Integer>();
            dfs(root, level, 0, maxLevel);
            if (level.size() == 0) {
                break;
            }
            
            results.add(level);
            maxLevel++;
        }
        
        return results;
    }
    
    private void dfs(TreeNode root,
                     List<Integer> level,
                     int curtLevel,
                     int maxLevel) {
        if (root == null || curtLevel > maxLevel) {
            return;
        }
        
        if (curtLevel == maxLevel) {
            level.add(root.val);
            return;
        }
        
        dfs(root.left, level, curtLevel + 1, maxLevel);
        dfs(root.right, level, curtLevel + 1, maxLevel);
    }
}

 

 

 


lintcode69- Binary Tree Level Order Traversal- easy

标签:==   ntc   www   traversal   node   ret   str   you   algo   

原文地址:http://www.cnblogs.com/jasminemzy/p/7707726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!