---
# System prepended metadata

title: 0543. Diameter of Binary Tree
tags: [Leetcode, FaceBook, DFS, Easy]

---

# 0543. Diameter of Binary Tree
###### tags: `Leetcode` `Easy` `FaceBook` `DFS`
Link: https://leetcode.com/problems/diameter-of-binary-tree/
## 思路
DFS 过程中记录最大值
一定要注意它找的不是经过几个node，要把经过的node个数-1
## Code
```java=
class Solution {
    int maxLen = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        dfs(root);
        return maxLen;
    }
    public int dfs(TreeNode root){
        if(root == null) return 0;
        int leftLen = dfs(root.left);
        int rightLen = dfs(root.right);
        maxLen = Math.max(leftLen+rightLen, maxLen);
        return Math.max(leftLen, rightLen)+1;
    }
}
```