85. Insert Node in a Binary Search Tree

Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree.

Example

Example 1:
Input: tree = {}, node = 1
Output: 1 Explanation:
Insert node 1 into the empty tree, so there is only one node on the tree. Example 2:
Input: tree = {2,1,4,3}, node = 6
Output: {2,1,4,3,6} Explanation:
Like this: 2 2
/ \ / \
1 4 --> 1 4
/ / \
3 3 6

Challenge

Can you do it without recursion?

思路:

在树上定位要插入节点的位置。

  1. 如果它大于当前根节点,则应该在右子树中,如果没有右子树则将该点作为右儿子插入;若存在右子树则在右子树中继续定位。
  2. 如果它小于当前根节点,则应该在左子树中,处理同上。

(二叉查找树中保证不插入已经存在的值)

二分法代码:

public TreeNode insertNode(TreeNode root, TreeNode node) {
if (root == null) {
return node;
}
if (root.val > node.val) {
root.left = insertNode(root.left, node);
} else {
root.right = insertNode(root.right, node);
}
return root;
}

Lintcode85-Insert Node in a Binary Search Tree-Easy的更多相关文章

  1. Lintcode: Insert Node in a Binary Search Tree

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  2. 85. Insert Node in a Binary Search Tree【easy】

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  3. LeetCode: 669 Trim a Binary Search Tree(easy)

    题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...

  4. 【leetcode】Convert Sorted Array to Binary Search Tree (easy)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...

  5. [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  6. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  7. [LeetCode] 701. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  8. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  9. 【leetcode】701. Insert into a Binary Search Tree

    题目如下: Given the root node of a binary search tree (BST) and a value to be inserted into the tree, in ...

随机推荐

  1. Hibernate处理事务并发问题

    在Hibernate中设置事务的隔离级别.<property name="hibernate.connection.isolation">2</property& ...

  2. php优秀的库

    PHP 是一种通用开源脚本语言.语法吸收了 C 语言.Java 和 Perl 的特点,利于学习,使用广泛,主要适用于 Web 开发领域,是大多数后端开发者的首选.PHP 作为最受欢迎的编程语言之一,经 ...

  3. 29个人,耗时84天,硬刚Python,实验结果如下。

    真有动漫风格的编程书籍? 上图,就是日本出版的编程书籍.为什么要搞成动漫风格?因为学编程常常会枯燥,难以坚持.法国思想家布封说:所谓天才,就是坚持不懈的意思.大家学编程,转行.涨薪.加强技能,无论是何 ...

  4. MFC DDX_Control 与 DDX_Text

    DDX_TEXT()的作用可以理解为把字符串变量和控件的文本(WindowText)关联起来, DDX_Control()的作用可以理解为把变量和控件本身关联起来, DoDataExchange(pD ...

  5. leaflet.toolbar.js

    leaflet.toolbar.js 参考:https://www.javascriptcn.com/read-38464.html

  6. mvc partialView+kendo window

    在写mvc项目时,一个列表查询页面含有多个操作按钮及弹框操作.原本写在了一个view中,导致代码繁多复杂,难以维护,还有表单赋值清空.验证等麻烦. 因此改用kendo window +partialV ...

  7. MVC 使用缓存

    public AController() { ViewBag[); } private List<BlogsClass> GetClass(int parentId) { List< ...

  8. c++基础 - constexpr

    const expression常量表达式,指值不会改变,并且在编译过程中就能得到计算结果的表达式. 复杂系统难以分辨一个初始值是否是常量表达式,因此提出constexptr以提示编译器,用来验证变量 ...

  9. Nginx 出现 _STORAGE_WRITE_ERROR_:./Runtime/Cache/Home/

    Nginx 出现  _STORAGE_WRITE_ERROR_:./Runtime/Cache/Home/ 这种情况是因为 application 没有足的权限 .需要给予777的权限就能解决了

  10. Matlab学以致用--模拟os任务装载情况

    2012-06-08 21:26:42 用matlab来建模,仿真不同时刻os task在队列中的装载情况.输入参数如下 作为初学者,M文件写的有点长.能实现功能就算学以致用了. clear;clc ...