Lintcode85-Insert Node in a Binary Search Tree-Easy
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?
思路:
在树上定位要插入节点的位置。
- 如果它大于当前根节点,则应该在右子树中,如果没有右子树则将该点作为右儿子插入;若存在右子树则在右子树中继续定位。
 - 如果它小于当前根节点,则应该在左子树中,处理同上。
 
(二叉查找树中保证不插入已经存在的值)
二分法代码:
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的更多相关文章
- 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 ...
 - 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 ...
 - 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 ...
 - 【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. 有序 ...
 - [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 ...
 - [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 ...
 - [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 ...
 - 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 ...
 - 【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 ...
 
随机推荐
- [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
			
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
 - 使用Apache JMeter对SQL Server、Mysql、Oracle压力测试(二)
			
接着第一篇的写: 第三步,测试SQL Server数据库的性能: a.加载JDBC SQL Server驱动.添加线程组和设置线程属性和第二步一样,就不再赘述了: b.设置JDBC Connectio ...
 - win 下 python   ImportError: No module named requests
			
第一次弄爬虫,报库找不到,网上找了半天,一般都让都让改成绝对路径...那不是饮鸩止渴嘛. 然后 在无意中发现,不需要控制台输入pip命令,因为不是在Linux下啊,,win下直接在pycharm里添加 ...
 - 10#Windows注册表的那些事儿
			
引言 用了多年的Windows系统,其实并没有对Windows系统进行过深入的了解,也正是由于Windows系统不用深入了解就可以简单上手所以才有这么多人去使用.笔者是做软件开发的,使用的基本都是Wi ...
 - UIElementImageShot
			
MemoryStream memStream = new MemoryStream(); System.Windows.Media.Imaging.RenderTargetBitmap bmp = n ...
 - Windows  system 在python文件操作时的路径表示方法
			
file_path =(r'i:\vacpy\ch10\pi_digits.txt') #将文件路径存储在变量file_path中with open (file_path) as file_objec ...
 - ls file  less
			
ls-a 列出所有文件,包含隐藏文件-l 以长格式显示结果-F 在所列出的文件后面显示其格式-r 按照文件名降序展示-t 按照时间列出-S 按照文件大小排序 file 文件名:展示文件的类型简单描述 ...
 - PHP获取汉字首字母函数
			
<?php function getFirstCharter($str) { if (empty($str)) { return ''; } $fchar = ord($str{0}); if ...
 - 父网访问子网(校园网访问校园网IP路由器下的一台电脑)远程路由器下的电脑
			
网路由器添加转发规则,端口转发,本人仅使用Pandora Box路由器固件 当然设置了这些还不够,还需要设置其他的允许端口转发的东西,例如 然后远程桌面的话还需要设置某些相关设置,例如电脑允许使用远程 ...
 - 编程规范(初尝ES6与webpack)
			
//针对ES6规范(第1-5条)start1.块级作用域let/const取代var:在let和const之间,建议优先使用const,尤其是在全局环境,不应该设置变量,只应设置常量. 2.解构赋值1 ...