Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / 
  9  20
    /  
   15   7

return its depth = 3.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

import java.util.Vector;

class Solution {
    public int maxDepth(TreeNode root) {
        int dep = 0;
		
        if (null != root) {
            Vector<TreeNode> vec = new Vector<>();
			
            vec.add(root);
            while (vec.size() > 0) {
                ++ dep;
                Vector<TreeNode> tmp = new Vector<>();
                for (TreeNode t : vec) {
                    if (null != t.left) tmp.add(t.left);
                    if (null != t.right) tmp.add(t.right);
                }
                vec = tmp;
            }
        }
		
        return dep;
    }
}

转载于:https://my.oschina.net/gonglibin/blog/1624136

原文地址:https://www.cnblogs.com/twodog/p/12137456.html