题目如下:

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.

Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.

For example,

Given the tree:
4
/ \
2 7
/ \
1 3
And the value to insert: 5

You can return this binary search tree:

         4
/ \
2 7
/ \ /
1 3 5

This tree is also valid:

         5
/ \
2 7
/ \
1 3
\
4

解题思路:因为题目对插入后树的高度没有任何约束,所以最直接的方法就是对树进行遍历。如果遍历到的当前节点值大于给定值,判断其节点的左子节点是否存在:不存在则将给定值插入到其左子节点,存在则往左子节点继续遍历;如果小于,则同理,判断其右子节点。直到找到符合条件的节点为止。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
loop = True
def recursive(self,node,val):
if self.loop == False:
return
if node.val > val:
if node.left == None:
node.left = TreeNode(val)
self.loop = False
else:
self.recursive(node.left,val)
else:
if node.right == None:
node.right = TreeNode(val)
self.loop = False
else:
self.recursive(node.right,val) def insertIntoBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
if root == None:
return TreeNode(val)
self.loop = True
self.recursive(root,val)
return root

【leetcode】701. Insert into a Binary Search Tree的更多相关文章

  1. 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【leetcode】Convert Sorted List to Binary Search Tree

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

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

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  5. 【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. 有序 ...

  6. 【leetcode】Convert Sorted List to Binary Search Tree (middle)

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. 【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路: ...

  8. 【leetcode】501. Find Mode in Binary Search Tree

    class Solution { public: vector<int> modes; int maxCnt = 0; int curCnt = 0; int curNum = 0; ve ...

  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, insert t ...

随机推荐

  1. HDU - 6601 Keen On Everything But Triangle 主席树

    Keen On Everything But Triangle 感觉最近多校好多主席树的亚子,但是本人菜得很,还没学过主席树,看着队友写题就只能划水,\(WA\)了还不能帮忙\(debug\),所以深 ...

  2. 【Linux】grep显示匹配行的上下几行的用法

    打印匹配行的前后5行 grep -5 ‘something’ file 打印匹配行的前后5行 grep -C 5 ‘something’ file 打印匹配行的后5行 grep -A 5 ‘somet ...

  3. A Guide To using IMU (Accelerometer and Gyroscope Devices) in Embedded Applications.

    介绍 本指南的目的是大家感兴趣的惯性MEMS(微机电系统)传感器,特别是加速计和陀螺仪和IMU组合设备(惯性测量单元). 例如IMU单位:Acc_Gyro_6DOF对MCU处理单元UsbThumb提供 ...

  4. Oracle 表空间、用户

    一.表空间 临时表空间 创建临时表空间范例 CREATE TEMPORARY TABLESPACE duke_temp /* 临时表空间名称:duke_temp */ tempfile 'C:\ora ...

  5. jenkins配置到gitlab拉代码

    参照: jenkins 从git拉取代码-简明扼要 https://www.cnblogs.com/jwentest/p/7065783.html 持续集成①安装部署jenkins从git获取代码-超 ...

  6. 家用NAS配置方案

    对家用用户而言,NAS即一台下载机,硬件需要满足以下几点: 1.稳定性:24×7稳定无故障运行. 2.拓展性:较多的硬盘槽位,便于容量扩容: 3.体积小巧:占地面积小,便于放置. 4.方便远程管理:无 ...

  7. 非常全的Linux基础知识点

    Linux是每个后端程序员必须要掌握的系统,今天小编就给你分享一篇Linux基础知识点大全,看看你知道多少? 一. 从认识操作系统开始 1.1 操作系统简介 我通过以下四点介绍什么操作系统: 操作系统 ...

  8. T1373:鱼塘钓鱼(fishing)

    原题链接:1373:鱼塘钓鱼(fishing) 解题思路: 由于在走路时,鱼的数量不会减少,那我们此时可以先减去路上可能花掉的时间,用剩下的时间来找最多的鱼,然后从左向右走,k枚举能到达的最远的鱼塘, ...

  9. Selenium:八种元素定位方法

    前言: 我们在做WEB自动化时,最根本的就是操作页面上的元素,首先我们要能找到这些元素,然后才能操作这些元素.工具或代码无法像我们测试人员一样用肉眼来分辨页面上的元素.那么我们怎么来定位他们呢? 在学 ...

  10. pssh系列命令详解

    安装 pssh提供OpenSSH和相关工具的并行版本.包括pssh,pscp,prsync,pnuke和pslurp.该项目包括psshlib,可以在自定义应用程序中使用.pssh是python写的可 ...