[B] [C] [D]

Given a binary tree, find its maximum depth.

The depth of a binary tree is the number of nodes along the longest path from the root node to the farthest leaf node.

Note: Leaf nodes are nodes that have no child nodes.

Example: given a binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
Copy the code

Returns its maximum depth of 3.

If the current node is a real node, try to use the current node depth to update the maximum depth and finally walk through the entire binary tree, which is the maximum depth of the binary tree

The code is as follows:

Var maxDepth = function(root) {let res = 0; Function preorder(node,deep){if(node === null) return; // If the current node is not empty, try updating the maximum depth with the current node depth res = math.max (res,deep); Preorder (node. Left,deep+1) preorder(node. Right,deep+1)} preorder(root,1); return res; };Copy the code

At this point we have the maximum depth of leetcode-104-binary tree

If you have any questions or suggestions, please leave a comment!