Leetcode solution 110: Balanced Binary Tree

Problem Statement

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example 1:

Given the following tree [3,9,20,null,null,15,7]:

    3
   /   9  20
    /     15   7

Return true.

Example 2:

Given the following tree [1,2,2,3,3,null,null,4,4]:

       1
      /      2   2
    /    3   3
  /  4   4

Return false.

Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

As described in the problem, it is intuitive to solve this problem recursively, especially given this is a tree related problem. What we can do is get the height of the left sub tree, compared with the right sub tree, then do the logics to see if it’s balanced or not. If at certain level either the left or right sub tree is not balanced, then entire Tree is not balanced. Classic usage for post order traversal.

Solutions

 1 public boolean isBalanced(TreeNode root) {
 2         return maxHeight(root) != -1;
 3     }
 4
 5     // if -1, means it is not a balanced tree, since it will also return the normal height(int), so boolean is not an option.
 6     // Kinda of a hack for the return type.
 7     // @return -1 means it‘s already not a balanced tree, else t;he tree height
 8     public int maxHeight(TreeNode root) {
 9         if (root == null) {
10             return 0;
11         }
12
13         int l = maxHeight(root.left);
14         int r = maxHeight(root.right);
15
16         // a classic usage of post order traversalß
17         if (l == -1 || r == -1 || Math.abs(l - r) > 1) {
18             return -1;
19         }
20         return Math.max(l, r) + 1;
21     }

Post order traversal using recursion

Calculate the height of the left subtree, calculate the height of the right subtree, then compare. If it‘s already not balanced, return -1 and directly return

Time Complexity: O(N), N is the total tree nodes since it is visited once and only once

Space Complexity: O(1), No extra space is needed

References

原文地址:https://www.cnblogs.com/baozitraining/p/11308254.html

时间: 2024-10-27 10:44:27

Leetcode solution 110: Balanced Binary Tree的相关文章

【一天一道LeetCode】#110. Balanced Binary Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of th

【LeetCode】110. Balanced Binary Tree

题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 提示: 此题要求判断一个给定的二叉树是否是“平衡”的,这里“平

110.Balanced Binary Tree Leetcode解题笔记

110.Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 很早以前做的了  准

LeetCode 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h

leetCode 110. Balanced Binary Tree 平衡二叉树

110. Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 题目大意: 判断一

【LeetCode】110. Balanced Binary Tree-判断是否为平衡二叉树

一.描述: 二.思路 平衡二叉树(Balanced Binary Tree):又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树: 通过每一棵左右子树的深度判断子树是否为平衡二叉树,只有当所有的子树是平衡二叉树时,才能得出整棵树是平衡二叉树: 故函数方法中有两种判断,1是空树?2左右两个子树的高度差的绝对值不超过1? 三.代码: 1 /** 2 * Definition for a binary tr

[LeetCode]题解(python):110 Balanced Binary Tree

题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never d

Leetcode[110]-Balanced Binary Tree

Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 判断一棵树是否属于平衡二叉树 判断主节点的左右节点深度大小差,如果不在

LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)

翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two