`
lhkzyz
  • 浏览: 346062 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java二叉树深度

阅读更多
//定义节点
	class Node{   
	    String key;   
	    Node left;   
	    Node right;   
	}  
 
//计算二叉树深度
	class NodeTree{   
	    public int getlength(Node root){   
	        int depthLeft=0;   
	        int depthRight=0;
	        int depth=0;
	        
	        //左子树的深度   
	        if(root.left!=null){
	        depthLeft= getlength(root.left)+1;   
	        }
	        
	        //右子树的深度   
	        if(root.right!=null){
	        depthRight= getlength(root.right)+1;
	        }
	        if(depthLeft>=depthRight){
	        	depth=depthLeft;
	        }
	        else {
	            depth=depthRight;
	        }
	        return depth;
	    }   
   }
 
0
7
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics