【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 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: 5You can return this binary search tree:
4
/ \
2 7
/ \ /
1 3 5This 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的更多相关文章
- 【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】501. Find Mode in Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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 ...
- 【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 ...
- 【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. 有序 ...
- 【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 ...
- 【题解】【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.思路: ...
- 【leetcode】501. Find Mode in Binary Search Tree
class Solution { public: vector<int> modes; int maxCnt = 0; int curCnt = 0; int curNum = 0; ve ...
- [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 ...
随机推荐
- 明明不太合适但是还是被用在配置文件和数据传输上的XML
XML概述: 概念: 可扩展的标记语言. 功能: 作为数据本地存储的格式.(已淘汰)作为结构化存储的方式,不如数据库效率高.目前一部分移动设备中还在使用. 作为网络中传输数据的格式.(已淘汰)作为网络 ...
- PHPthink 配置目录
系统默认的配置文件目录就是应用目录(APP_PATH),也就是默认的application下面,并分为应用配置(整个应用有效)和模块配置(仅针对该模块有效). ├─application 应用目录 │ ...
- PHP curl_close函数
说明 void curl_close ( resource $ch ) 关闭一个cURL会话并且释放所有资源.cURL句柄ch 也会被释放. 参数 ch 由 curl_init() 返回的 cURL ...
- S1.2 Python开发规范指南
参考链接 Python风格规范 分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Pytho ...
- [HDU2294]Pendant
题目:Pendant 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2294 分析: 1)f[i][j]表示长度为i,有j种珍珠的吊坠的数目. $f[i][ ...
- Java中static修饰类的问题
Java中static修饰类的问题 众所周知,Java中static关键字可以修饰方法与变量: 修饰变量的时候,这个变量属于类变量,可以直接通过类名.变量名来引用. 修饰方法的时候可以直接通过类名.方 ...
- ARM与单片机到底有啥区别
1.软件方面 这应该是最大的区别了.引入了操作系统.为什么引入操作系统?有什么好处? 1)方便.主要体现在后期的开发,即在操作系统上直接开发应用程序.不像单片机一样一切都要重新写.前期的操 ...
- Hooking EndScene
Hey guys, umm i was trying to hook endscene using detours and i used a method that i hooked many oth ...
- linux从head.s到start_kernelstart_kernel之---内核解压到重定位分析
一: arm linux 内核生成过程 1. 依据arch/arm/kernel/vmlinux.lds 生成linux内核源码根目录下的vmlinux,这个vmlinux属于未压缩,带调试信息.符号 ...
- 五. jenkins部署springboot项目(2)--windows环境--服务
前提:jenkins和springboot运行在同一台机器 springboot 通过winsw部署为服务 winsw 下载地址:https://github.com/kohsuke/winsw/re ...