【LeetCode 110_二叉树_遍历】Balanced Binary Tree

解法一:From top to bottom

 1 int treeHeight(TreeNode *T)
 2 {
 3     if (T == NULL)
 4         return 0;
 5
 6     return max(treeHeight(T->left), treeHeight(T->right)) + 1;
 7 }
 8
 9 bool isBalanced(TreeNode* root)
10 {
11     if (root == NULL)
12         return true;
13
14     int left_height = treeHeight(root->left);
15     int right_height = treeHeight(root->right);
16     if (abs(left_height - right_height) > 1)
17         return false;
18
19     if (!isBalanced(root->left) || !isBalanced(root->right))
20         return false;
21
22     return true;
23 }

解法一递归判断子树是否为平衡二叉树的过程中,重复计算了多次子树的高度,时间复杂度大于O(N)。解法二将这个计算过程优化了,时间复杂度为O(N)。

解法二:From bottom to top

 1 int dfsTreeHeight(TreeNode *T)
 2 {
 3     if (T == NULL)
 4         return 0;
 5
 6     int left_height = dfsTreeHeight(T->left);
 7     if (left_height == -1)
 8         return -1;
 9     int right_height = dfsTreeHeight(T->right);
10     if (right_height == -1)
11         return -1;
12     if (abs(left_height - right_height) > 1)
13         return -1;
14
15     return max(left_height, right_height) + 1;
16 }
17
18 bool isBalanced(TreeNode* root)
19 {
20     if (root == NULL)
21         return true;
22
23     return dfsTreeHeight(root) != -1;
24 }
时间: 2024-12-28 20:51:41

【LeetCode 110_二叉树_遍历】Balanced Binary Tree的相关文章

【LeetCode 144_二叉树_遍历】Binary Tree Preorder Traversal

解法一:非递归 1 vector<int> preorderTraversal(TreeNode* root) 2 { 3 vector<int> res; 4 if (root == NULL) 5 return res; 6 7 stack<TreeNode*> node_stack; 8 TreeNode *p = root; 9 while (p || !node_stack.empty()) { 10 if (p) { 11 res.push_back(p-&

【LeetCode 100_二叉树_遍历】Same Tree

解法一:递归 1 bool isSameTree(TreeNode* p, TreeNode* q) 2 { 3 if (p == NULL && q == NULL) 4 return true; 5 if ((p == NULL && q != NULL) || (p != NULL && q == NULL)) 6 return false; 7 if (p->val != q->val) 8 return false; 9 10 if (

LeetCode 199. 二叉树的右视图(Binary Tree Right Side View)

199. 二叉树的右视图 199. Binary Tree Right Side View 题目描述 给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值. Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. LeetCode19

【Leetcode】【Easy】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. 错误的思路(o(N2)时间复杂度): 写一个函数a,用递归遍历的方法,

【LeetCode 104_二叉树_遍历】Maximum Depth of Binary Tree

解法一:递归 1 int maxDepth(TreeNode* root) 2 { 3 if (root == NULL) 4 return 0; 5 6 int max_left_Depth = maxDepth(root->left); 7 int max_right_Depth = maxDepth(root->right); 8 return max(max_left_Depth, max_right_Depth) + 1; 9 } 解法二:BFS 1 int maxDepth(Tre

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 OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined

LeetCode——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]Balanced Binary Tree @ Python

原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/ 题意:判断一颗二叉树是否是平衡二叉树. 解题思路:在这道题里,平衡二叉树的定义是二叉树的任意节点的两颗子树之间的高度差小于等于1.这实际上是AVL树的定义.首先要写一个计算二叉树高度的函数,二叉树的高度定义为:树为空时,高度为0.然后递归求解:树的高度 = max(左子树高度,右子树高度)+1(根节点要算上).高度计算函数实现后,递归求解每个节点的左右子树的高度差,如果有大于1的