lintcode 85 在二叉树中插入节点
来源:互联网 发布:win10禁用windows键 编辑:程序博客网 时间:2024/06/03 13:35
给定一棵二叉查找树和一个新的树节点,将节点插入到树中。
你需要保证该树仍然是一棵二叉查找树。
2.利用二叉查找树的特性,根据插入节点的值和根节点的值进行比较,若比根节点的值小则在根的左子树上插入,否则在右子树上插入。
3./**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: The root of the binary search tree.
* @param node: insert this node into the binary search tree
* @return: The root of the new binary search tree.
*/
TreeNode* insertNode(TreeNode* root, TreeNode* node) {
// write your code here
if(root==NULL)
return node;
if(root->val>=node->val)
root->left = insertNode(root->left,node);
if(root->val<node->val)
root->right = insertNode(root->right,node);
return root;
}
};
4.比较简单的一道题.,能通过老师讲的思路做出来
- lintcode-在二叉查找树中插入节点-85
- LintCode(85)在二叉查找树中插入节点
- LintCode 85-在二叉查找树中插入节点
- lintcode 85 在二叉树中插入节点
- LintCode:在二叉查找树中插入节点
- lintcode ----在二叉查找树中插入节点
- 【lintcode笔记】在二叉查找树中插入节点
- lintcode——在二叉查找树中插入节点
- LintCode-在二叉查找树中插入节点
- lintcode 在二叉查找树中插入节点
- LintCode 在二叉查找树中插入节点
- 在二叉查找树中插入节点-LintCode
- LintCode 在二叉查找树中插入节点
- 在二叉查找树中插入节点-LintCode
- LintCode 85. 在二叉查找树中插入节点
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
- lintcode insert-node-in-a-binary-search-tree 在二叉查找树中插入节点
- LintCode(S)二叉查找树插入节点
- 高级网络配置及ipv6网络的管理
- poj2488
- 为了学习,找些好伙伴吧
- Python3教程--协程(转自廖雪峰的官方网站)
- Hibernate的主键生成策略
- lintcode 85 在二叉树中插入节点
- 跟我学TCP/IP系列4
- Imu数据均值滤波分析
- 什么是优先级?
- mvcc 数据库相关
- eclipse 自带git插件 文件提交后修改标志不明显
- 腾讯云cors配置
- 四种色彩模式ARGB_8888、ARGB_4444、 RGB_565、 ALPHA_8的区别
- 2017计蒜客第二场(AB)