104. Maximum Depth of Binary Tree

  题目:Given a binary tree, find its maximum depth. 计算二叉树的深度。

  解决:采用深度优先遍历。

  

 1 package com.wang.test;
 2 
 3 /*
 4  * @tittle:104. Maximum Depth of Binary Tree
 5  * @auther:wwwglin
 6  * @time:2016/04/13
 7  */
 8 public class MaximumDepthofBinaryTree {
 9     public int maxDepth(TreeNode root) {
10         //深度优先遍历
11         if (root == null) {
12             return 0;
13         }
14         int l = maxDepth(root.left);
15         int r = maxDepth(root.right);
16         return l > r ? l + 1 : r + 1;
17     }
18 
19     static class TreeNode {
20         int val;
21         TreeNode left;
22         TreeNode right;
23 
24         TreeNode(int x) {
25             val = x;
26         }
27     }
28 }

 

posted @ 2016-04-13 15:14  wwwglin  阅读(115)  评论(0)    收藏  举报