File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change 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 === null ) return 0 ;
15+
16+ let queue = [ root ] ;
17+ let depth = 0 ;
18+
19+ while ( queue . length > 0 ) {
20+ let levelSize = queue . length ;
21+ for ( let i = 0 ; i < levelSize ; i ++ ) {
22+ const node = queue . shift ( ) ;
23+ if ( node . left ) queue . push ( node . left ) ;
24+ if ( node . right ) queue . push ( node . right ) ;
25+ }
26+ depth ++ ;
27+ }
28+
29+ return depth ;
30+ } ;
You can’t perform that action at this time.
0 commit comments