lintcode insert-node-in-a-binary-search-tree 在二叉查找树中插入节点
来源:互联网 发布:h5页面分享到朋友圈js 编辑:程序博客网 时间:2024/06/08 10:22
问题描述
http://www.lintcode.com/zh-cn/problem/insert-node-in-a-binary-search-tree/
笔记
当root节点为空,表示可以插入node,因此直接返回node。
如果node->val < root->val,应该在左子树插入。否则在右子树插入。然后返回root。
代码
/** * 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 (node->val < root->val) { root->left = insertNode(root->left, node); return root; } root->right = insertNode(root->right, node); return root; }};
0 0
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
- lintcode insert-node-in-a-binary-search-tree 在二叉查找树中插入节点
- 85.Insert Node in a Binary Search Tree-在二叉查找树中插入节点(容易题)
- lintcode: Insert Node in a Binary Search Tree
- Insert Node in a Binary Search Tree
- Insert Node in a Binary Search Tree
- [刷题]Insert Node in a Binary Search Tree
- #85 Insert Node in a Binary Search Tree
- Lintcode85 Insert Node In A Binary Search Tree solution 题解
- lintcode-在二叉查找树中插入节点-85
- LintCode:在二叉查找树中插入节点
- lintcode ----在二叉查找树中插入节点
- LintCode(85)在二叉查找树中插入节点
- LintCode 85-在二叉查找树中插入节点
- 【lintcode笔记】在二叉查找树中插入节点
- lintcode——在二叉查找树中插入节点
- LintCode-在二叉查找树中插入节点
- lintcode 在二叉查找树中插入节点
- java 基础之Map用法
- 调用系统照相机和相册
- mmap与直接IO(read、write)的效率比较
- MVC 全局菜单权限过滤器
- 关于数据序列化(3),使用JSON的方式,以及几个注意的点,以及fastJson的坑
- lintcode insert-node-in-a-binary-search-tree 在二叉查找树中插入节点
- 排序算法一览(上):交换类、选择类和插入类排序
- mybatis中 大于小于的写法
- bing87496988 ~ onvif笔记~~soap编程流程
- swift使用FMDB数据库增删改查
- HDU 5781 ATM Mechine( 多校5 DP&期望+记忆化搜索)
- 2015 偶数求和
- 一个简单的AsyncTast小例子
- unix系列设备驱动(字符设备驱动) 学习