235. Lowest Common Ancestor of a Binary Search Tree

  • 当我们从上向下去递归遍历,第一次遇到 cur节点是数值在[p, q]区间中,那么cur就是 p和q的最近公共祖先
  • 不需要遍历整棵树,找到结果直接返回 -> 这里是一条边的写法
  • 方法1: Recursion
  • Time Complexity: O(n). In the worst case, we might be visiting all the nodes of the binary tree.
  • Space Complexity: O(n). In the worst case, the recursion stack would be N since the height of a skewed binary tree could be N.
class Solution {public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if (root == null || p == root || q == root) return root;TreeNode left = lowestCommonAncestor(root.left, p, q);TreeNode right = lowestCommonAncestor(root.right, p, q);if (left != null && right != null) return root;if (left == null) return right;return left;}
}
  • 方法2: Iteration - 比Binary Tree的要简单,利用只走一边的特性

701. Insert into a Binary Search Tree

  • 方法1: Recursion
  • Time Complexity:  O(H), where H is a tree height. That results in O(log⁡N) in the average case and O(N) in the worst case.
  • Space Complexity: O(H)
class Solution {public TreeNode insertIntoBST(TreeNode root, int val) {if (root == null) return new TreeNode(val);if (val > root.val) root.right = insertIntoBST(root.right, val);else root.left = insertIntoBST(root.left, val);return root;}
}// 没有必要特别考虑leafclass Solution {public TreeNode insertIntoBST(TreeNode root, int val) {if (root == null) return new TreeNode(val);if (root.left == null && root.right == null) {if (root.val < val) root.right = new TreeNode(val);else root.left = new TreeNode(val);return root;}if (val > root.val) root.right = insertIntoBST(root.right, val);else root.left = insertIntoBST(root.left, val);return root;}
}
  • 方法2: Iteration - 注意BST的iteration和BT的inorder traversal iteration完全不一样
  • 利用只走一边的特性
class Solution {public TreeNode insertIntoBST(TreeNode root, int val) {if (root == null) return new TreeNode(val);TreeNode newRoot = root;TreeNode pre = root;while (root != null) {pre = root;if (root.val > val) {root = root.left;} else if (root.val < val) {root = root.right;} }if (pre.val > val) {pre.left = new TreeNode(val);} else {pre.right = new TreeNode(val);}return newRoot;}
}

450. Delete Node in a BST

  • 五种情况:

    • 1. 没找到key
    • 2. root.val == key && left == null && right == null
    • 3. root.val == key && left == null
    • 4. root.val == key && right == null
    • 5. root.val == key && left != null && right != null
      • 将删除节点的左子树放到删除节点的右子树的最左面节点的左孩子的位置。并返回删除节点右孩子为新的根节点。
class Solution {public TreeNode deleteNode(TreeNode root, int key) {if (root == null) return null;if (root.val == key) {if (root.left == null) return root.right;else if (root.right == null) return root.left;else {TreeNode cur = root.right;while (cur.left != null) {cur = cur.left;}cur.left = root.left;root = root.right;return root;}}if (root.val > key) root.left = deleteNode(root.left, key);else root.right = deleteNode(root.right, key);return root;}
}

第22天 Binary Tree 235、701、450相关推荐

  1. 力扣235|701|450

    235. 二叉搜索树的最近公共祖先 - 力扣(LeetCode) 思路 不用改树结构,直接找到空结点位置插入 类似之前的二叉树的公共祖先,由二叉树最下面进行遍历,由下往上,特别关注的是二叉搜索树是一个 ...

  2. day22 二叉树| 235,701,450

    235. 二叉搜索树的最近公共祖先 可以按照二叉树的最近公共祖先进行操作 也可以按照搜索树的特征,无需进行回溯,从上到下进行遍历 701.二叉搜索树中的插入操作 ​​​​​​​将固定的数值插入到合适的 ...

  3. 代码随想录算法训练营第22天 二叉树 java :235. 二叉树的最近公共祖先 701.二叉搜索树中的插入操作 450.删除二叉搜索树中的节点

    文章目录 LeetCode 236. 二叉树的最近公共祖先 题目讲解 思路 LeetCode 701.二叉搜索树中的插入操作 题目讲解 思路 LeetCode 450.删除二叉搜索树中的节点 题目讲解 ...

  4. leetcode 235. Lowest Common Ancestor of a Binary Search Tree | 235. 二叉搜索树的最近公共祖先(哈希表)

    题目 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 题解 哈希表解法思路来自左程云< ...

  5. 22 最近共同先祖(Lowest Common Ancestor of a Binary Tree)

    文章目录 1 题目 2 解决方案 2.1 思路 2.2 图解 2.3 时间复杂度 2.4 空间复杂度 3 源码 3.1 遍历法 1 题目 题目:最近共同先祖(Lowest Common Ancesto ...

  6. Lowest Common Ancestor of a Binary Search Tree a Binary Tree

    235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common ...

  7. [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  8. Balanced Binary Tree leetcode java

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  9. [leetcode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...

最新文章

  1. lt view gt android,RecyclerView的linearLayoutManager.findViewByPosition()问题
  2. Effective C++ -- 零散知识点整理
  3. cve-2015-0569 安卓手机提权ROOT漏洞 分析
  4. 一文整理四六级考前必背知识点【精简版】
  5. python属性错误怎么改_属性错误:无法设置属性
  6. 可爱妈妈对我的\情色\教育
  7. 完全卸载go语言编译器
  8. xEasyApp之后端的介绍
  9. 计算机入门建模观后感,实习生revit学习心得-初学Revit有感
  10. 如何面试前端工程师?
  11. 经典Hello Word窗口表示,可视化编程入门。
  12. MYSQL 8.0 OCP
  13. 【mmcv】——CNN
  14. 绿色到黄色到红色的颜色渐变
  15. 相见恨晚的编程学习词典!谁还不是南极滑冰的那个崽儿?!
  16. Apache详解(一)Internet和HTTP协议
  17. python爬虫中遇到“\xb5”、“xa0”等字符时报错编码错误的处理方式
  18. Matlab App Designer 函数写法
  19. 快速查询出中通快运物流信息,将信息导出EXCEL表格
  20. java ollydbg_OllyDBG

热门文章

  1. 用Canvas实现刮刮卡功能的研究与实践
  2. PS怎么设计一张彩图
  3. python 凝聚层次聚类_关于层次聚类算法的python实现
  4. 批量删除pdf文档中的注释(是一个网址列表)
  5. Linux工控主板内核及开机画面的在线更新
  6. 开发一款单词app需要多少钱?
  7. oppo 手机侧滑快捷菜单_你见过微信侧滑返回的联动效果,但开门效果、百叶窗效果见过吗?...
  8. DTOJ 4841. 岩雀
  9. 生活没有波澜(转美文)
  10. Java语言中的接口与实现