We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9ea2b26 commit cc9b88cCopy full SHA for cc9b88c
1 file changed
maximum-depth-of-binary-tree/Cyjin-jani.js
@@ -0,0 +1,28 @@
1
+/**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
10
+ * @param {TreeNode} root
11
+ * @return {number}
12
13
+var maxDepth = function (root) {
14
+ if (!root) return 0;
15
+ let answer = 0;
16
+
17
+ function dfs(node, count) {
18
+ answer = Math.max(answer, count);
19
20
+ if (!node.left && !node.right) return;
21
22
+ if (node.left) dfs(node.left, count + 1);
23
+ if (node.right) dfs(node.right, count + 1);
24
+ }
25
+ dfs(root, 1);
26
27
+ return answer;
28
+};
0 commit comments