Java代码分别用递归和非递归方式计算二叉树的最大深度

转自http://blog.csdn.net/snow_7/article/details/51818580

递归实现
为了求树的深度,可以先求其左子树的深度和右子树的深度,可以用递归实现,递归的出口就是节点为空。返回值为0

代码:

public class Deep  
{  
    //递归实现1  
  public int findDeep(BiTree root)  
  {  
      int deep = 0;  
      if(root != null)  
      {  
          int lchilddeep = findDeep(root.left);  
          int rchilddeep = findDeep(root.right);  
          deep = lchilddeep > rchilddeep ? lchilddeep + 1 : rchilddeep + 1;  
      }  
      return deep;  
  }  

//递归实现2

 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;//左子树和右子树深度较大的那个加一等于整个树的深度  
      }  
  }  

非递归实现
*利用层次遍历的算法,设置变量level记录当前节点所在的层数,设置变量last指向当前层的最后一个节点,当处理完当前层的最后一个节点,让level指向+1操作。设置变量cur记录当前层已经访问的节点的个数,当cur等于last时,表示该层访问结束。
层次遍历在求树的宽度、输出某一层节点,某一层节点个数,每一层节点个数都可以采取类似的算法。
树的宽度:在树的深度算法基础上,加一个记录访问过的层节点个数最多的变量max,在访问每层前max与last比较,如果max比较大,max不变,如果max小于last,把last赋值给max;*

//非递归实现

 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;  
  }  
  public static void main(String[] args)  
 {  
    BiTree root = BiTree.buildTree();  
    Deep deep = new Deep();  
    System.out.println(deep.findDeep(root));  
    System.out.println(deep.findDeep1(root));  
    System.out.println(deep.findDeep2(root));     
 }  
}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。 先序遍历: - 递归实现: ``` void preOrderTraversal(TreeNode* root) { if(root == nullptr) return; cout << root->val << " "; preOrderTraversal(root->left); preOrderTraversal(root->right); } ``` - 非递归实现: ``` void preOrderTraversal(TreeNode* root) { if(root == nullptr) return; stack<TreeNode*> s; s.push(root); while(!s.empty()) { TreeNode* cur = s.top(); s.pop(); cout << cur->val << " "; if(cur->right) s.push(cur->right); if(cur->left) s.push(cur->left); } } ``` 中序遍历: - 递归实现: ``` void inOrderTraversal(TreeNode* root) { if(root == nullptr) return; inOrderTraversal(root->left); cout << root->val << " "; inOrderTraversal(root->right); } ``` - 非递归实现: ``` void inOrderTraversal(TreeNode* root) { if(root == nullptr) return; stack<TreeNode*> s; TreeNode* cur = root; while(cur != nullptr || !s.empty()) { while(cur != nullptr) { s.push(cur); cur = cur->left; } cur = s.top(); s.pop(); cout << cur->val << " "; cur = cur->right; } } ``` 后序遍历: - 递归实现: ``` void postOrderTraversal(TreeNode* root) { if(root == nullptr) return; postOrderTraversal(root->left); postOrderTraversal(root->right); cout << root->val << " "; } ``` - 非递归实现: ``` void postOrderTraversal(TreeNode* root) { if(root == nullptr) return; stack<TreeNode*> s; s.push(root); TreeNode* pre = nullptr; while(!s.empty()) { TreeNode* cur = s.top(); if((cur->left == nullptr && cur->right == nullptr) || (pre != nullptr && (pre == cur->left || pre == cur->right))) { cout << cur->val << " "; s.pop(); pre = cur; } else { if(cur->right != nullptr) s.push(cur->right); if(cur->left != nullptr) s.push(cur->left); } } } ``` 以上就是递归非递归方式实现二叉树的先序、中序和后序遍历的代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值