求二叉树的深度 递归 非递归

来源:互联网 发布:windows更新卡住了 编辑:程序博客网 时间:2024/05/29 04:33
 public int findDeep1(BiTree root)    {              if(root == null)        {            return 0;        }        else        {         int lchilddeep = findDeep1(root.left);//求左子树的深度         int rchilddeep = findDeep1(root.left);//求右子树的深度         return lchilddeep > rchilddeep ? lchilddeep + 1 : rchilddeep + 1;//左子树和右子树深度较大的那个加一等于整个树的深度        }    }  





  public int findDeep2(BiTree root)    {       if(root == null)           return 0;             BiTree current = null;       LinkedList<BiTree> queue = new LinkedList<BiTree>();       queue.offer(root);       int cur,last;       int level = 0;       while(!queue.isEmpty())       {           cur = 0;//记录本层已经遍历的节点个数           last = queue.size();//当遍历完当前层以后,队列里元素全是下一层的元素,队列的长度是这一层的节点的个数           while(cur < last)//当还没有遍历到本层最后一个节点时循环           {               current = queue.poll();//出队一个元素               cur++;               //把当前节点的左右节点入队(如果存在的话)               if(current.left != null)               {                   queue.offer(current.left);               }               if(current.right != null)               {                   queue.offer(current.right);               }           }           level++;//每遍历完一层level+1       }       return level;    } 

原创粉丝点击