leetcode701】的更多相关文章

Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. No…
https://www.cnblogs.com/grandyang/p/9914546.html 类似于二分查找的方法,用迭代的方法去做 注意:无论是进入左子树还是右子树,左右子树都变成了新的数,所以需要重新根据root->left = ....来重新生成 class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { if(root == NULL) return new TreeNode(val); if…
class Solution: def insertIntoBST(self, root, val): """ Time: O(log(n)) [average case] Space: O(1) """ new_node = TreeNode(val) if not root: return new_node curr = root while True: if curr.val > val: if not curr.left: curr…
给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树. 返回插入后二叉搜索树的根节点. 保证原始二叉搜索树中不存在新值. 注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可. 你可以返回任意有效的结果. 例如, 给定二叉搜索树: 4 / \ 2 7 / \ 1 3 和 插入的值: 5 你可以返回这个二叉搜索树: 4 / \ 2 7 / \ / 1 3 5 或者这个树也是有效的: 5 / \ 2 7 / \ 1 3 \ 4 /** * Definition fo…
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的需要考虑root是否是范围内合理的起点.其他细节:每次需要引用节点值时考虑是否非空. 基本概念: 二叉树节点的深度:从上数第几层:指从根->该节点的最长简单路径边的条数. 二叉树节点的高度:从下数第几层:指从节点->叶子节点的最长简单路径边的条数. leetcode144.二叉树的前序遍历 递归较…
分门别类刷算法,坚持,进步! 刷题路线参考:https://github.com/youngyangyang04/leetcode-master 大家好,我是拿输出博客来督促自己刷题的老三,这一节我们来刷二叉树,二叉树相关题目在面试里非常高频,而且在力扣里数量很多,足足有几百道,不要慌,我们一步步来.我的文章很长,你们 收藏一下. 二叉树基础 二叉树是一种比较常见的数据结构,在开始刷二叉树之前,先简单了解一下一些二叉树的基础知识.更详细的数据结构知识建议学习<数据结构与算法>. 什么是二叉树…