zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    1002 写出这个数
    1001 害死人不偿命的(3n+1)猜想
    Graph I
    Tree
    进程通信
    管道
    fork函数
    Priority Queue
    Search
    游戏 slider
  • 原文地址:https://www.cnblogs.com/twodog/p/12137459.html
Copyright © 2011-2022 走看看