Topic
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along, the longest path from the root node to the farthest leaf node.
Code
/** * 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); }}
code Download: Https://github.com/jimenbian/GarvinLeetCode
/********************************
* This article from the blog "Bo Li Garvin"
* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod
******************************************/
"Leetcode from zero single row" No104 Maximum Depth of Binary Tree