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. jQuery-AutoComplete自动提示简单实现

    注:本次案列实现功能为 用户注册信息,如果数据库对应表中存在部分信息,点击已有的用户的用户名,自动补全其它已有的基本信息 实现思路:通过AutoComplete提示,异步通过用户名查询全表,充当Aut ...

  2. TCP/IP协议 网络层

    IP协议介绍 1.IP协议是TCP/IP协议族中最为核心的协议.IP协议将多个包交换网络连接起来,它在源地址和目的地址之间传送一种称为数据包的东西,它还提供对数据大小的重新组装功能,以适应不同网络对包 ...

  3. .Net Core:Middleware中间件管道

    .NetCore中的Middleware是装配到管道处理请求和响应的组件:每个组件都可以决定是否继续进入下一个管道.并且可以在进入下一个管道前后执行逻辑: 最后一个管道或者中断管道的中间件叫终端中间件 ...

  4. vue-devtools的安装与使用

    安装 vue.js devtools vue官方中文文档:http://www.uihtm.com/vue/v2/guide/index.html# 安装 vue.js devtools 前提是Goo ...

  5. 图表管理账单的NABCD

    首先,我们团队的项目目标是记账本.就我个人理解,记账本中心功能有两项,第一,记录:第二,显示.而本篇博客主要描述用各种不同的图表来显示的NABCD. 首先是N(need),用户的需求就是我们的动力!利 ...

  6. [React Native] change port when running react native

    Two ways to do that. First, use this module to do that, https://github.com/ktonon/react-native-port- ...

  7. 解决 flannel.1 interface state DOWN

    ip a 查看结果  flannel.1 i state UNKNOWN 并且五 inet 发现日志 device (flannel.1): state change: unmanaged -> ...

  8. QT 添加外部库文件

    LIBS += D:\Code\Opengltest\OpenGL32.Lib D:\Code\Opengltest\GlU32.Lib LIBS += OpenGL32.Lib GlU32.Lib  ...

  9. Thanks David's Share

    2019.4.12 Today, we got the invitation letter from US, when we discuss the journey, i saw perfect sh ...

  10. fiddler 抓包工具(新猿旺学习总结)

    安装抓包工具 Fiddler 直接安装 fiddler下载连接:https://www.lanzous.com/i30k09c 设置 fiddler 因为 r fiddler 是抓取 P HTTP 和 ...