逐层宽度优先搜索(BFS)二叉树各种解法

9 篇文章 0 订阅
8 篇文章 0 订阅

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).

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.

Tags   Expand  

BFS宽度优先搜索遍历二叉树实事求是是基本的不能再基本的基本的基本功了。
以前只会在纸上画一画。
今天终于能耐下心来自己跑一跑代码了。
原题地址:http://www.lintcode.com/en/problem/binary-tree-level-order-traversal/


直接bruteforce怎么奢侈怎么来

用两个数组a1 a2

第一个数字放树的根节点,然后遍历第一个数组,所有节点的左右子节点放到第二个数组。然后清空第一个数组。

遍历第二个数组a2,每个节点的左右子节点再放到第一个数组a1。然后清空第二个数组。

简单粗暴血淋淋。

Talk is cheap,show you the code


/**
 * 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: The root of binary tree.
     * @return: Level order a list of lists of integer
     */
    public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        // write your code here
        
      ArrayList<ArrayList<Integer>> result =new ArrayList<ArrayList<Integer>>();
      
      if(root == null){
          return result;
      }
      ArrayList<TreeNode> a1 = new ArrayList<TreeNode>();
      ArrayList<TreeNode> a2 = new ArrayList<TreeNode>();
      
      a1.add(root);
      
      while( a1.size()!= 0 ){
          a2.clear();
          ArrayList<Integer> b = new ArrayList<Integer>();
          for(int i = 0; i<a1.size(); i++){
              b.add(a1.get(i).val);
          }
          
          for(int i = 0;i<a1.size();i++){
              if(a1.get(i).left != null){
                  a2.add(a1.get(i).left);
              }
              if(a1.get(i).right != null){
                  a2.add(a1.get(i).right);
              }
          }
          
          ArrayList<TreeNode> Tep = new ArrayList<TreeNode>();
          Tep = a1;
          a1 = a2;
          a2 = Tep;
          
          
          result.add(b);
          
          
          
      }
      return result;
        
        
    }
}


后来听说有BFS可以通过DFS深度优先搜索的方式解决。觉得非常的惊讶。在以往的只是储备中觉得DFS和BFS是水火不相容的事情。通过一些资料才知道原来以前在内存只有X MB的那个年代,都是采取时间换空间的方式。应为BFS太奢侈了需要消耗很大空间,所以有大神通过控制DFS深度优先搜索的层数的方式来实现了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: The root of binary tree.
     * @return: Level order a list of lists of integer
     */
    public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
        // write your code here
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if(root == null){
            return result;
        }
       int maxDepth = 0;
       while(true){
           ArrayList<Integer> tepResult = new ArrayList<Integer>();
           DFS(root , tepResult , 0 , maxDepth);
           if(tepResult.size()==0){
               break;
           }
           result.add(tepResult);
           maxDepth++;
       }
        return result;
        
    }
    
    public void DFS(TreeNode root , ArrayList<Integer> result , int curDepth , int maxDepth){
        if(root == null || curDepth > maxDepth){
            return;
        }
        
        if(curDepth == maxDepth){
            //add result
            result.add(root.val);
            return;
        }
        DFS(root.left , result , curDepth+1 , maxDepth);//left
        DFS(root.right , result , curDepth+1 , maxDepth);//right
    }
    
    
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值