在二叉查找树中插入节点-LintCode
来源:互联网 发布:mac版的photoshop cc 编辑:程序博客网 时间:2024/06/08 13:49
给定一棵二叉查找树和一个新的树节点,将节点插入到树中。
你需要保证该树仍然是一棵二叉查找树。
样例:
给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的:
#ifndef C85_H#define C85_H#include<iostream>using namespace std;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; int i = 0; TreeNode *p = root,*q=root; while (p != NULL) { if (p->val < node->val) { q = p; p = p->right; i = 0; } else { q = p; p = p->left; i = 1; } } if (i == 0) q->right = new TreeNode(node->val); else q->left = new TreeNode(node->val); return root; }};#endif
阅读全文
0 0
- lintcode-在二叉查找树中插入节点-85
- LintCode:在二叉查找树中插入节点
- lintcode ----在二叉查找树中插入节点
- LintCode(85)在二叉查找树中插入节点
- LintCode 85-在二叉查找树中插入节点
- 【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)二叉查找树插入节点
- 在二叉查找树中插入节点
- 自定义相机旋转拍照角度
- MySQL分组查询时出现错误SELECT list is not in GROUP BY ;this is incompatible with sql_mode=only_full_group_by
- ppt后续更新模板(二)说明
- 练习赛ming最优安排(贪心)
- testNG入门教程
- 在二叉查找树中插入节点-LintCode
- PHP递归无限分类
- 用RecyclerView多布局实现购物车+随便逛一逛
- JavaScript的prototype
- python将某一目录下的多个文件合并为一个文件
- DrawerLayout滑动禁止Toolbar点击监听控制
- 关于倒排索引
- 俄国沙皇问题笔记
- ehcache memcache redis