104. Maximum Depth of Binary Tree

来源:互联网 发布:网络上sn是什么意思啊 编辑:程序博客网 时间:2024/05/02 01:49

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.

DFS

public class Solution {    public int maxDepth(TreeNode root) {        if(root == null) return 0;        int left = 0,right = 0;        if(root.left != null){            left = maxDepth(root.left);        }         if(root.right != null){            right = maxDepth(root.right);        }        return right >= left ? right+1:left+1;    }}
0 0
原创粉丝点击