LeetCode 题解(4):二叉树-递归法
learn from cyc2018 and LeetCode
1 | // Definition for a binary tree node. |
类似于链表,二叉树的很多问题也可以利用递归的方法简单地实现。
104. Maximum Depth of Binary Tree (easy)
问题描述
得到二叉树的深度。
题解
1 | class Solution { |
相关问题
110. Balanced Binary Tree (easy)
111. Minimum Depth of Binary Tree (easy)
559. Maximum Depth of N-ary Tree (easy)
110. Balanced Binary Tree (easy)
问题描述
判断一个树是否是平衡的:
如果任意一个结点的两个子树高度差小于等于 1,那么该树是平衡的。
题解
1 | class Solution { |
相关问题
104. Maximum Depth of Binary Tree (easy)
543. Diameter of Binary Tree (easy)
问题描述
求二叉树的直径:即任意两个结点之间的最长距离。
题解
1 | class Solution { |
相关问题
687. Longest Univalue Path (easy)
687. Longest Univalue Path (easy)
问题描述
寻找二叉树的最长路径,其中该路径上所有的结点值相同。
题解
1 | class Solution { |
相关问题
543. Diameter of Binary Tree (easy)
124. Binary Tree Maximum Path Sum (hard)
226. Invert Binary Tree (easy)
问题描述
将所有的左右子树翻转。
题解
1 | class Solution { |
617. Merge Two Binary Trees (easy)
问题描述
将两棵二叉树归并到一棵新的二叉树中。其中归并方法为:相同位置的结点值相加(如果其中一个结点为 null,则进行覆盖)。
题解
1 | class Solution { |
112. Path Sum (easy)
问题描述
给定一个值与一个二叉树,判断是否存在一个由根到叶子结点的路径,使得该路径所有结点之和等于给定值。
题解
1 | class Solution { |
相关问题
124. Binary Tree Maximum Path Sum (hard)
129. Sum Root to Leaf Numbers (medium)
437. Path Sum III (easy)
问题描述
寻找二叉树中满足如下条件的路径的个数:
该路径上所有值的和等于某特定值。
题解
1 | class Solution { |
相关问题
687. Longest Univalue Path (easy)
572. Subtree of Another Tree (easy)
问题描述
给定两棵非空二叉树 $s$ 和 $t$ 。判断 $t$ 是否与 $s$ 的某一子树完全相同( $t$ 是否是 $s$ 的一棵子树)。
题解
1 | class Solution { |
相关问题
508. Most Frequent Subtree Sum (medium)
101. Symmetric Tree (easy)
问题描述
判断一棵二叉树是否是左右对称的。
题解
1 | class Solution { |
111. Minimum Depth of Binary Tree (easy)
问题描述
给定一个二叉树,寻找其最小深度(从根节点到所有叶子结点中的最短距离)。
题解
1 | class Solution { |
相关问题
102. Binary Tree Level Order Traversal (medium)
104. Maximum Depth of Binary Tree (easy)
404. Sum of Left Leaves (easy)
问题描述
求二叉树所有左叶子结点值之和。
题解
1 | class Solution { |
337. House Robber III (medium)
问题描述
一个小偷进入了一个新的区域来偷东西,这个区域由一个二叉树构成。小偷意识到,一天晚上不能同时偷两个直接相连的房间,如果二叉树的值表示小偷能在这个房间偷到的钱数,那么判断今晚小偷能在这片区域偷到的总金额。
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that “all houses in this place forms a binary tree”. It will automatically contact the police if two directly-linked houses were broken into on the same night.
Determine the maximum amount of money the thief can rob tonight without alerting the police.
题解
1 | class Solution { |
相关问题
671. Second Minimum Node In a Binary Tree (easy)
问题描述
有一种特殊的二叉树,该树上所有的结点要么有 0 个孩子,要么有 2 个孩子,并且子结点的值一定大于等于父结点,那么求该二叉树上第二小的值为多少,如果不存在第二小的值,则返回 -1。
题解
1 | class Solution { |
相关问题
230. Kth Smallest Element in a BST (medium)
235. Lowest Common Ancestor of a Binary Search Tree (easy)
问题描述
给定一个二叉搜索树,寻找两个结点的最近共同祖先(LCA)。
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
题解
对于给定的两个子结点 $p$ 和 $q$, 它们有三种可能:
- 分别在根结点的左右子树中:那么显然 LCA 就是根节点。
- 均在左子树中:那么在左子树中递归。
- 均在右子树中:那么在右子树中递归。
1 | class Solution { |