public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { //创建一个类型为TreeNode 的队列, 另外创建一个类型为Integer的ArrayList用于存放结果 Queue<TreeNode> q = new LinkedList <> (); ArrayList<Integer> res = new ArrayList<> (); if(root == null) return res; // 注意此处的res是一...
判断树中结点个数 public static int getTreeNum(TreeNode root) return getTreeNum(root.leftChild) + getTreeNum(root.rightChild) + 1; 1. 判断树的深度 public static int getTreeDepth(TreeNode root) int leftDepth = getTreeDepth(root.leftChild) + 1; int rightDepth = getTreeDepth(root.rightC...
LinkedList<Integer> temp =newLinkedList<>(); // 使用 for 循环,将 queue 中的元素按照给定的规则添加的 temp 中 for(inti =0; i < size; i++) { // 从 queue 中取出一个节点 TreeNode node = queue.poll(); // 如果是奇数层,那么按顺序添加到双端队列的尾部 if(isOddNumber) { temp.addLast(...
1publicvoidlevelOrder(TreeNode tree){ 2Queue<TreeNode>queue=newLinkedList<>; 3queue.add(tree); 4intlevel =0;//统计有多少层 5while(!queue.isEmpty) { 6//每一层的节点数 7intsize =queue.size; 8for(inti =0; i < size; i++) { 9TreeNode node =queue.poll; 10//打印节点 11System....
LinkedList 实现了:List、Deque(双向链表结构)、Queue(队列接口)。 1、Deque LinkedList <xxx> ll =newLinkedList<xxx>(); ll.addFirst("q0");//头部加入ll.addLast("q1");// 尾部加入System.out.println(ll.getFirst());//查看最前面的对象System.out.println(ll.getLast());//查看最后面的对象System...
在addBefore 方法中无非就是做了这件事:构建一个新节点 newEntry,然后修改其前后的引用。 LinkedList还提供了其他的增加方法: add(int index, E element):在此列表中指定的位置插入指定的元素。 addAll(Collection 1.5 移除方法 remove(Object o):从此列表中移除首次出现的指定元素(如果存在)。该方法的源代码如下...
public boolean add(E e) { addBefore(e, header); return true; } 该方法调用addBefore方法,然后直接返回true,对于addBefore()而已,它为LinkedList的私有方法。 private Entry<E> addBefore(E e, Entry<E> entry) { //利用Entry构造函数构建一个新节点 newEntry, Entry<E> newEntry = new Entry<E>(e...
Initializes a new instance of the Queue<T> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied. Namespace: System.Collections.Generic Assembly: System (in System.dll) Syntax VB Copy 'Declaration Public Sub New...
该方法首先会判断移除的元素是否为 null,然后迭代这个链表找到该元素节点,最后调用remove(Entry e),remove(Entry e) 为私有方法,是 LinkedList 中所有移除方法的基础方法,如下: privateEremove(Entry<E>e){if(e==header)thrownewNoSuchElementException();//保留被移除的元素:要返回Eresult=e.element;//将该节点...
IEqualityComparer(T) Interface IList(T) Interface ISet(T) Interface KeyedByTypeCollection(TItem) Class KeyNotFoundException Class KeyValuePair(TKey, TValue) Structure LinkedList(T) Class LinkedList(T).Enumerator Structure LinkedListNode(T) Class ...