平衡二叉树 题目:输入一棵二叉树,判断该二叉树是否是平衡二叉树。 在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树 平衡二叉树(Balanced Binary Tree),具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。 func ...
分类:
其他好文 时间:
2021-04-12 11:48:28
阅读次数:
0
根据题目意思,实际上我们计算出左右子树的高度相加后即为结果(实际还需-1) 1.深度优先遍历 时间O(n),空间O(h) class Solution { int max= 0; public int diameterOfBinaryTree(TreeNode root) { def(root); ...
分类:
其他好文 时间:
2021-04-12 11:46:11
阅读次数:
0
二叉树——236. 二叉树的最近公共祖先 题目: 思路: 后续遍历+DFS:具体思路参照了题解中的解析 代码: class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* ...
分类:
其他好文 时间:
2021-04-09 13:25:04
阅读次数:
0
一.二叉树的前序遍历 https://leetcode-cn.com/problems/binary-tree-preorder-traversal/ class Solution { public List<Integer> preorderTraversal(TreeNode root) { L ...
分类:
其他好文 时间:
2021-04-01 13:41:51
阅读次数:
0
1 class Solution { 2 private: 3 int maxSum = INT_MIN; 4 5 public: 6 int maxGain(TreeNode* node) { 7 if (node == nullptr) { 8 return 0; 9 } 10 11 // 递归 ...
分类:
其他好文 时间:
2021-04-01 13:30:06
阅读次数:
0
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # 递归 # 时间复杂度:O(n ...
分类:
其他好文 时间:
2021-03-29 12:51:26
阅读次数:
0
输入一棵二叉树和一个整数,打印出二叉树中节点值的和为输入整数的所有路径。从树的根节点开始往下一直到叶节点所经过的节点形成一条路径; /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode * ...
分类:
其他好文 时间:
2021-03-29 12:41:37
阅读次数:
0
void flatten(TreeNode root) { // base case,即最小 if (root == null) return; flatten(root.left); flatten(root.right); /**** 后序遍历 ****/ // 1、保存原来的左右节点,避免变换 ...
分类:
其他好文 时间:
2021-03-16 11:50:33
阅读次数:
0
问题 请完成一个函数,输入一个二叉树,该函数输出它的镜像。 示例 解答1:递归 class Solution { public: TreeNode* mirrorTree(TreeNode* root) { if (!root) return nullptr; // 前序操作 swap(root-> ...
分类:
其他好文 时间:
2021-03-11 17:54:57
阅读次数:
0
问题: 给定一棵二叉树。 求给定节点target开始,距离K的所有节点。 Example 1: Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2 Output: [7,4,1] Explanation: The nodes ...
分类:
其他好文 时间:
2021-03-09 13:09:46
阅读次数:
0