Balanced Binary Tree LeetCode solution in JavaScript

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 {boolean}
12
*/
13
var isBalanced = function (root) {
14
let result = true;
15
function Dfs(root) {
16
if (!root) return 0;
17
let left = Dfs(root.left);
18
let right = Dfs(root.right);
19
if (Math.abs(left - right) > 1) {
20
result = false;
21
return;
22
}
23
return 1 + Math.max(left, right);
24
}
25
Dfs(root);
26
return result;
27
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0