leetcode-104-二叉树的最大深度(maximum depth of binary tree)-

题目及用例

package pid104;


/*二叉树的最大深度

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。



}*/
public class main {
	
	public static void main(String[] args) {
		Object[] x=new Object[]{3,9,20,null,null,15,7};	
		BinaryTree tree=new BinaryTree(x);
		tree.printTree(tree.root);
		test(tree.root);
		
		
	}
		 
	private static void test(TreeNode ito) {
		Solution solution = new Solution();
		int rtn;
		long begin = System.currentTimeMillis();
		rtn = solution.maxDepth(ito);//执行程序
		long end = System.currentTimeMillis();		
		System.out.println("rtn=" );
		System.out.print(rtn);
		System.out.println();
		System.out.println("耗时:" + (end - begin) + "ms");
		System.out.println("-------------------");
	}

}

树与节点的类

package pid104;

public class TreeNode {

	int val;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int x) {
		val = x; 
		left=null;
		right=null;
		}
}

package pid104;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTree {

	public TreeNode root;
	public List<TreeNode> nodes=new ArrayList<TreeNode>();
	
	public BinaryTree(int x){
		root=new TreeNode(x);
		nodes.add(root);
	}
	public BinaryTree(Object[] x){		
		for(int i=0;i<x.length;i++){			
			if(x[i]!=null){
				nodes.add(new TreeNode((int)x[i]));
			}
			else{
				nodes.add(null);
			}
		}
		root=nodes.get(0);
		for(int i=0;i<x.length/2;i++){
			TreeNode now=nodes.get(i);
			if(now!=null){
				now.left=nodes.get(2*i+1);
				now.right=nodes.get(2*i+2);
			}
		}
		
	}
	
	public void preOrder(TreeNode root){
		if(root==null){
			return;
		}
		System.out.print(root.val+" ");
		preOrder(root.left);
		preOrder(root.right);
	}
	
	public void inOrder(TreeNode root){
		if(root==null){
			return;
		}
		inOrder(root.left);
		System.out.print(root.val+" ");
		inOrder(root.right);
	}
	
	public void postOrder(TreeNode root){
		if(root==null){
			return;
		}
		postOrder(root.left);
		postOrder(root.right);
		System.out.print(root.val+" ");
	}
	
	public void printTree(TreeNode root){
        if(root == null)
            return;
        Queue<TreeNode> queue = new LinkedList<TreeNode>();
        
        int current;//当前层 还未打印的结点个数
        int next;//下一层结点个数
        
        queue.offer(root);
        current = 1;
        next = 0;
        while(!queue.isEmpty()){
            TreeNode currentNode = queue.poll();
            if (currentNode!=null) {
            	System.out.print(currentNode.val+" ");
                current--;
               
			}
            else{
            	System.out.print("null ");
            	 current--;
            	 queue.offer(null);
                 next++;
                 queue.offer(null);
                 next++;                
                 if(current ==0){
                     System.out.println();
                     current = next;
                     next = 0;
                     int temp=0;
                     for (TreeNode treeNode : queue) {
						if(treeNode==null){
							temp++;
						}
					}
                     if(temp==current){
                    	 System.out.println("end");
                         break;
                     }
                     
                 }
                continue;
            }
            
            if(currentNode.left != null){
                queue.offer(currentNode.left);
                next++;
            }
            else{
            	queue.offer(null);
                next++;
            }
            if(currentNode.right != null){
                queue.offer(currentNode.right);
                next++;
            }
            else{
            	queue.offer(null);
                next++;
            }
            if(current ==0){
                System.out.println();
                current = next;
                next = 0;
                int temp=0;
                for (TreeNode treeNode : queue) {
					if(treeNode==null){
						temp++;
					}
				}
                if(temp==current){
               	 System.out.println("end");
                    break;
                }
                
            }
            
        }
    }
	
	
}

解法1(成功,2ms,较慢)

建立两个list,now和next
now的初始值为root
length初始为0
每次循环
将,now的left与right加入next
然后让now=next,并且length++
直到now为null为止

package pid104;

import java.util.LinkedList;
import java.util.List;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        List<TreeNode> now=new LinkedList<TreeNode>();
        List<TreeNode> next=new LinkedList<TreeNode>();
    	if(root==null){
    		return 0;
    	}
    	int length=0;
    	now.add(root);
    	
    	while(!now.isEmpty()){
    	for (TreeNode treeNode : now) {
			if(treeNode.left!=null){
				next.add(treeNode.left);
			}
			if(treeNode.right!=null){
				next.add(treeNode.right);
			}
		}
        now=next;
        next=new LinkedList<TreeNode>();
        length++;
    	}
        
    	return length;
    }
}

解法2(成功,1ms,极快)
递归算法,
长度=当前长度(1,因为自己有)+左右的max长度
如果左右都为空,则都会返回0,那么长度=1+max(0,0)=1

递归求解,递归公式
n就是treenode参数
  f(n) = 0; n=null,
  f(n) = 1+ max(f(n左), f(n右))

	public int maxDepth(TreeNode root) {
        if(root==null){
        	return 0;
        }
        return 1+Math.max(maxDepth(root.left), maxDepth(root.right));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值