【LeetCode从零单排】No104 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.

代码

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    List<Integer> deep_list=new ArrayList<Integer>();
    public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        getDepth(root,1);
        int max=1;
        for(int i=0;i<deep_list.size()-1;i++){
            if(deep_list.get(i)>max){
                max=deep_list.get(i);
            }

        }
        return max;
    }
    public void getDepth(TreeNode root,int height){
        if(root==null)
        {
            deep_list.add(height-1) ;
            return;
        }
        getDepth(root.left,height+1);
        getDepth(root.right,height+1);
    }
}

代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客  “李博Garvin“

* 转载请标明出处:http://blog.csdn.net/buptgshengod

******************************************/

时间: 2024-07-28 15:38:51

【LeetCode从零单排】No104 Maximum Depth of Binary Tree的相关文章

【LeetCode从零单排】No 114 Flatten Binary Tree to Linked List

题目 Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解题思路:利用递归找到倒数第一个父节点,记录下它的右节点,将左边的移到右边,然后再把之前标记的右节点连接上. 代码 public class Solution { public

[LeetCode]104.Maximum Depth of Binary Tree

[题目] Maximum Depth of Binary Tree  Total Accepted: 5260 Total Submissions: 11532My Submissions 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 n

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution 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

LeetCode 104 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. 代码 /** * Definition for a binary tre

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. 提交的错误,不知道为什么,但是要自己避免.   C++实现代码: #include<iostream> #include<new> #include<vecto

[LeetCode]111.Minimum Depth of Binary Tree

[题目] Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. [分析] 类似于:LeetCode之Maximum Depth of Binary Tree [代码] /********************************

[LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level

Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.   注意与求树的高度的区别.   C++实现代码: #include<iostream> #include<new> #include<vector> u

【LeetCode从零单排】No198.House Robber &amp;amp;&amp;amp;No91.Decode Ways&amp;amp;&amp;amp;139 word break(动态规划典型应用)

1.题目 一道典型的Dynamic Programming的题目. You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security