返回介绍

lcci / 04.04.Check Balance / README_EN

发布于 2024-06-17 01:04:43 字数 4555 浏览 0 评论 0 收藏 0

04.04. Check Balance

中文文档

Description

Implement a function to check if a binary tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that the heights of the two subtrees of any node never differ by more than one.


Example 1:


Given tree [3,9,20,null,null,15,7]

  3

   / \

  9  20

  /  \

   15   7

return true.

Example 2:


Given [1,2,2,3,3,null,null,4,4]

    1

   / \

  2   2

   / \

  3   3

 / \

4   4

return false.

 

Solutions

Solution 1

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, x):
#     self.val = x
#     self.left = None
#     self.right = None


class Solution:
  def isBalanced(self, root: TreeNode) -> bool:
    def dfs(root: TreeNode):
      if root is None:
        return 0, True
      a, b = dfs(root.left)
      c, d = dfs(root.right)
      return max(a, c) + 1, abs(a - c) <= 1 and b and d

    return dfs(root)[1]
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *   int val;
 *   TreeNode left;
 *   TreeNode right;
 *   TreeNode(int x) { val = x; }
 * }
 */
class Solution {
  public boolean isBalanced(TreeNode root) {
    return dfs(root) >= 0;
  }

  private int dfs(TreeNode root) {
    if (root == null) {
      return 0;
    }
    int l = dfs(root.left);
    int r = dfs(root.right);
    if (l < 0 || r < 0 || Math.abs(l - r) > 1) {
      return -1;
    }
    return Math.max(l, r) + 1;
  }
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
  bool isBalanced(TreeNode* root) {
    function<int(TreeNode*)> dfs = [&](TreeNode* root) {
      if (!root) {
        return 0;
      }
      int l = dfs(root->left);
      int r = dfs(root->right);
      if (l == -1 || r == -1 || abs(l - r) > 1) {
        return -1;
      }
      return max(l, r) + 1;
    };
    return dfs(root) >= 0;
  }
};
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *   Val int
 *   Left *TreeNode
 *   Right *TreeNode
 * }
 */
func isBalanced(root *TreeNode) bool {
  var dfs func(*TreeNode) int
  dfs = func(root *TreeNode) int {
    if root == nil {
      return 0
    }
    l, r := dfs(root.Left), dfs(root.Right)
    if l == -1 || r == -1 || abs(l-r) > 1 {
      return -1
    }
    return max(l, r) + 1
  }
  return dfs(root) >= 0
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *   val: number
 *   left: TreeNode | null
 *   right: TreeNode | null
 *   constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 *   }
 * }
 */

function isBalanced(root: TreeNode | null): boolean {
  const dfs = (root: TreeNode | null): number => {
    if (!root) {
      return 0;
    }
    const l = dfs(root.left);
    const r = dfs(root.right);
    if (l === -1 || r === -1 || Math.abs(l - r) > 1) {
      return -1;
    }
    return Math.max(l, r) + 1;
  };
  return dfs(root) >= 0;
}

Solution 2

# Definition for a binary tree node.
# class TreeNode:
#   def __init__(self, x):
#     self.val = x
#     self.left = None
#     self.right = None


class Solution:
  def isBalanced(self, root: TreeNode) -> bool:
    def dfs(root: TreeNode):
      if root is None:
        return 0
      l, r = dfs(root.left), dfs(root.right)
      if l == -1 or r == -1 or abs(l - r) > 1:
        return -1
      return max(l, r) + 1

    return dfs(root) >= 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文