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

版权声明:此文章为许诗宇所写,如需转载,请写下转载文章的地址 https://blog.csdn.net/xushiyu1996818/article/details/82757310

题目及用例

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(别人的)
精妙的递归算法,
长度=当前长度(1,因为自己有)+左右的max长度

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

public class Solution {

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        } else if (root.left == null && root.right == null) {
            return 1;
        } else {
            int left = maxDepth(root.left);
            int right = maxDepth(root.right);
            return 1 + (left > right ? left : right);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/xushiyu1996818/article/details/82757310