如何创建二叉树[3,9,20,null,null,15,7],以便将其传递到levelorder方法中?

j5fpnvbx  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(340)

我试图做一个水平顺序遍历,并希望通过这个二叉树

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

在这个方法中

public List<List<Integer>> levelOrder(TreeNode root) {

但这行不通。

TreeNode bt = [3,9,20,null,null,15,7];
    levelOrder(bt);

这是levelorder方法

public List<List<Integer>> levelOrder(TreeNode root) {
    Queue<TreeNode> queue = new LinkedList<TreeNode>();
    List<List<Integer>> wrapList = new LinkedList<List<Integer>>();

    if(root == null) return wrapList;

    queue.offer(root);
    while(!queue.isEmpty()){
        int levelNum = queue.size();
        List<Integer> subList = new LinkedList<Integer>();
        for(int i=0; i<levelNum; i++) {
            if(queue.peek().left != null) queue.offer(queue.peek().left);
            if(queue.peek().right != null) queue.offer(queue.peek().right);
            subList.add(queue.poll().val);
        }
        wrapList.add(subList);
    }
    return wrapList;
}

这是树节点

public class TreeNode {
 int val;
 TreeNode left;
 TreeNode right;
 TreeNode(int x) { val = x; }
}
lyfkaqu1

lyfkaqu11#

你必须创建每一个你想创建的树节点。

TreeNode bt = new TreeNode(3);
 TreeNode a = new TreeNode(9);
 TreeNode b = new TreeNode(20);
 TreeNode c = new TreeNode(15);
 TreeNode d = new TreeNode(7);

 bt.left = a;
 bt.right = b;
 bt.right.left = c;
 bt.right.right = d;

或者,您可以创建一个树节点构造函数,它接受一个整数数组并适当地创建一个树节点。

vsnjm48y

vsnjm48y2#

希望这会有帮助

public class TreeTest
{
    public static void main(String[] args)
    {
        TreeNode tree = new TreeNode( 3 );
        int[] nums = {9,20,5,8,15,7};
        for(int i : nums ) {
        tree.addNode( i );
        }

        System.out.println(TreeTest.levelOrder(tree));
    }
    public static  List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        List<List<Integer>> wrapList = new LinkedList<List<Integer>>();

        if(root == null) return wrapList;

        queue.offer(root);
        while(!queue.isEmpty()){
            int levelNum = queue.size();
            List<Integer> subList = new LinkedList<Integer>();
            for(int i=0; i<levelNum; i++) {
                if(queue.peek().left != null) queue.offer(queue.peek().left);
                if(queue.peek().right != null) queue.offer(queue.peek().right);
                subList.add(queue.poll().val);
            }
            wrapList.add(subList);
        }
        return wrapList;
    }

}
class TreeNode
{
     int val;
     TreeNode left;
     TreeNode right;
     TreeNode(int x)
     {       
         val = x; 
         this.left = null;
         this.right = null;
     }
     public void addNode(int num) 
     {
         if (num < this.val)
         {
             if (this.left != null) 
                 this.left.addNode(num);
             else 
                 this.left = new TreeNode(num);
         } 
         else 
         {
             if (this.right != null) 
                 this.right.addNode(num);
             else
                 this.right = new TreeNode(num);             
         }
     }
}

相关问题