/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{publicint[]levelOrder(TreeNoderoot){if(root==null){returnnewint[]{};}Deque<TreeNode>q=newArrayDeque<>();q.offer(root);List<Integer>res=newArrayList<>();while(!q.isEmpty()){for(intn=q.size();n>0;--n){TreeNodenode=q.poll();res.add(node.val);if(node.left!=null){q.offer(node.left);}if(node.right!=null){q.offer(node.right);}}}int[]ans=newint[res.size()];for(inti=0;i<ans.length;++i){ans[i]=res.get(i);}returnans;}}
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funclevelOrder(root*TreeNode)(ans[]int){ifroot==nil{return}q:=[]*TreeNode{root}forlen(q)>0{forn:=len(q);n>0;n--{node:=q[0]q=q[1:]ans=append(ans,node.Val)ifnode.Left!=nil{q=append(q,node.Left)}ifnode.Right!=nil{q=append(q,node.Right)}}}return}
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */publicclassSolution{publicint[]LevelOrder(TreeNoderoot){if(root==null){returnnewint[]{};}Queue<TreeNode>q=newQueue<TreeNode>();q.Enqueue(root);List<int>ans=newList<int>();while(q.Count!=0){intx=q.Count;for(inti=0;i<x;i++){TreeNodenode=q.Dequeue();ans.Add(node.val);if(node.left!=null){q.Enqueue(node.left);}if(node.right!=null){q.Enqueue(node.right);}}}returnans.ToArray();}}