1. 程式人生 > >LeetCode104_MaximumDepthofBinaryTree Java題解

LeetCode104_MaximumDepthofBinaryTree Java題解

from margin popu gin nodes down bsp neu ont

題目:

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.

解題:

求最大深度 和前面一題類似 用遞歸遍歷就能夠


代碼:

public static int maxDepth(TreeNode root) {
		if(root==null)//遞歸返回或結束條件
			return 0;
		else {
			return 1+Math.max(maxDepth(root.left),maxDepth(root.right) );
		}
        
    }


LeetCode104_MaximumDepthofBinaryTree Java題解