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. 迪杰斯特拉(Dijkstra)算法描述及理解

    Dijkstra算法是一种计算单源最短无负边路径问题的常用算法之一,时间复杂度为O(n2) 算法描述如下:dis[v]表示s到v的距离,pre[v]为v的前驱结点,用以输出路径,vis[v]表示该点最 ...

  2. DateTimeFormat

    中文:星期一,星期二 System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetDayName(DateTime.Now.Da ...

  3. Django系统

    #Django系统 -环境 - python3.6 - django1.8 -参考资料 - [django中文教程](http://python.usyiyi.cn) - django架站的16堂课 ...

  4. TCP/IP协议 数据链路层

    以太网 1.以太网(Ethernet)是一种计算机局域网技术,由Xerox.Intel公司在1982年联合开发的技术规范. 2.IEEE组织的IEEE 802.3标准制定了以太网的技术标准,它规定了包 ...

  5. 2019春第七周作业Compile Summarize

    这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 在这里 我在这个课程的目标是 能更加进一步的够熟练掌握指针的用法 这个作业在那个具体方面帮助我实现目标 指针对于基础题目的做法 参考文献与 ...

  6. Oracle 12C CRS-5013

    1.背景 OS:SUSE 12SP3 DB:12.2.0.1.190115 2节点RAC Q:crs alert日志一直刷如下报错 2019-02-12 12:46:18.163 [ORAAGENT( ...

  7. python虚拟环境迁移

    python虚拟环境迁移: 注意事项:直接将虚拟环境复制到另一台机器,直接执行是会有问题的. 那么可以采用以下办法: 思路:将机器1虚拟环境下的包信息打包,之后到机器2上进行安装: (有两种情况要考虑 ...

  8. linux----------linux下安装rar和unrar命令

    1.wget http://www.rarlab.com/rar/rarlinux-x64-4.2.0.tar.gz 2.tar xf rarlinux-x64-4.2.0.tar.gz    解压下 ...

  9. docker容器实战-----初级<2>

    第二章  docker容器 1. Docker是通过内核虚拟化技术(namespaces及cgroups cpu.内存.磁盘io等)来提供容器的资源隔离与安全保障等.由于Docker通过操作系统层的虚 ...

  10. Redis批量导入数据的方法

    有时候,我们需要给redis库中插入大量的数据,如做性能测试前的准备数据.遇到这种情况时,偶尔可能也会懵逼一下,这里就给大家介绍一个批量导入数据的方法. 先准备一个redis protocol的文件( ...