力扣hot100 二叉树的最大深度 dfs

‍ 题目地址

AC code

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
	public int maxDepth(TreeNode root)
	{
		int d = 1;
		if (root == null)
			return 0;
		int max = 0;
		if (root.right != null)
			max = Math.max(maxDepth(root.right), max);
		if (root.left != null)
			max = Math.max(maxDepth(root.left), max);

		return d + max;
	}
}

你可能感兴趣的:(力扣,hot100,leetcode,深度优先,算法)