leetcode

Posted 友哥

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode相关的知识,希望对你有一定的参考价值。

/**
 * 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>> iListList= new ArrayList<List<Integer>>();
        int depth=0;
        exploreTreeNode(root,iListList,depth );
        return iListList;
        
    }
    
    public void exploreTreeNode(TreeNode root, List<List<Integer>> iListList, int depth){
        
        if(root==null) return;
        if( depth >= iListList.size() ){
            List<Integer> iList = new ArrayList<Integer>();
            iListList.add(depth, iList);
            iList.add(root.val);
        }
        else{
            iListList.get(depth).add(root.val);
        }
        if(null!=root.left){
           exploreTreeNode(root.left, iListList , depth+1); 
        }
        if(null!=root.right){
           exploreTreeNode(root.right, iListList , depth+1); 
        }
    }
    
}

 

以上是关于leetcode的主要内容,如果未能解决你的问题,请参考以下文章