LeetCode[257] 二叉树的所有路径

Read more

LeetCode[110] 平衡二叉树


Related Topics:
“树”: https://leetcode.com/tag/tree/
“深度优先搜索”: https://leetcode.com/tag/depth-first-search/
“二叉树”: https://leetcode.com/tag/binary-tree/
Similar Questions:
“二叉树的最大深度”: https://leetcode.com/problems/maximum-depth-of-binary-tree/

Problem:

给定一个二叉树,判断它是否是高度平衡的二叉树。

本题中,一棵高度平衡二叉树定义为:

一个二叉树每个节点的左右两个子树的高度差的绝对值不超过 1 。

示例 1:

1
2
输入:root = [3,9,20,null,null,15,7]
输出:true

示例 2:

1
2
输入:root = [1,2,2,3,3,null,null,4,4]
输出:false

示例 3:

1
2
输入:root = []
输出:true

提示:

  • 树中的节点数在范围 [0, 5000]
  • -104 <= Node.val <= 104
Read more

LeetCode[222] 完全二叉树的节点个数

Related Topics:
“树”: https://leetcode.com/tag/tree/
“深度优先搜索”: https://leetcode.com/tag/depth-first-search/
“二分查找”: https://leetcode.com/tag/binary-search/
“二叉树”: https://leetcode.com/tag/binary-tree/
Similar Questions:
“最接近的二叉搜索树值”: https://leetcode.com/problems/closest-binary-search-tree-value/

Problem:

给你一棵完全二叉树 的根节点 root ,求出该树的节点个数。

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

示例 1:

1
2
输入:root = [1,2,3,4,5,6]
输出:6

示例 2:

1
2
输入:root = []
输出:0

示例 3:

1
2
输入:root = [1]
输出:1

提示:

  • 树中节点的数目范围是[0, 5 * 104]
  • 0 <= Node.val <= 5 * 104
  • 题目数据保证输入的树是 完全二叉树

进阶:遍历树来统计节点是一种时间复杂度为 O(n) 的简单解决方案。你可以设计一个更快的算法吗?

Read more

LeetCode[104] 二叉树的最大深度


Related Topics:
“树”: https://leetcode.com/tag/tree/
“深度优先搜索”: https://leetcode.com/tag/depth-first-search/
“广度优先搜索”: https://leetcode.com/tag/breadth-first-search/
“二叉树”: https://leetcode.com/tag/binary-tree/
Similar Questions:
“平衡二叉树”: https://leetcode.com/problems/balanced-binary-tree/
“二叉树的最小深度”: https://leetcode.com/problems/minimum-depth-of-binary-tree/
“N 叉树的最大深度”: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/

Problem:

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7]

1
2
3
4
5
3
/ \
9 20
/ \
15 7

返回它的最大深度 3 。

Read more

LeetCode[102] 二叉树的层序遍历

Related Topics:
“树”: https://leetcode.com/tag/tree/
“广度优先搜索”: https://leetcode.com/tag/breadth-first-search/
“二叉树”: https://leetcode.com/tag/binary-tree/
Similar Questions:
“二叉树的锯齿形层序遍历”: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
“二叉树的层序遍历 II”: https://leetcode.com/problems/binary-tree-level-order-traversal-ii/
“二叉树的最小深度”: https://leetcode.com/problems/minimum-depth-of-binary-tree/
“二叉树的垂直遍历”: https://leetcode.com/problems/binary-tree-vertical-order-traversal/
“二叉树的层平均值”: https://leetcode.com/problems/average-of-levels-in-binary-tree/
“N 叉树的层序遍历”: https://leetcode.com/problems/n-ary-tree-level-order-traversal/
“二叉树的堂兄弟节点”: https://leetcode.com/problems/cousins-in-binary-tree/

Problem:

给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。

示例:
二叉树:[3,9,20,null,null,15,7],

1
2
3
4
5
3
/ \
9 20
/ \
15 7

返回其层序遍历结果:

1
2
3
4
5
[
[3],
[9,20],
[15,7]
]
Read more