lintcode-在二叉查找树中插入节点-85
来源:互联网 发布:淘宝亿次元 编辑:程序博客网 时间:2024/05/22 13:57
给定一棵二叉查找树和一个新的树节点,将节点插入到树中。
你需要保证该树仍然是一棵二叉查找树。
样例
给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的:
2 2 / \ / \1 4 --> 1 4 / / \ 3 3 6
挑战
能否不使用递归?
/** * 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: TreeNode* insertNode(TreeNode* root, TreeNode* node) { if(!node) return root; if(!root) return root=node; TreeNode *cur=root; while(cur){ if(node->val==cur->val) return root; else if(node->val<cur->val){ if(!cur->left){ cur->left=node; return root; } cur=cur->left; }else{ if(!cur->right){ cur->right=node; return root; } cur=cur->right; } } }};
0 0
- lintcode-在二叉查找树中插入节点-85
- LintCode(85)在二叉查找树中插入节点
- LintCode 85-在二叉查找树中插入节点
- LintCode:在二叉查找树中插入节点
- lintcode ----在二叉查找树中插入节点
- 【lintcode笔记】在二叉查找树中插入节点
- lintcode——在二叉查找树中插入节点
- LintCode-在二叉查找树中插入节点
- lintcode 在二叉查找树中插入节点
- LintCode 在二叉查找树中插入节点
- 在二叉查找树中插入节点-LintCode
- LintCode 在二叉查找树中插入节点
- 在二叉查找树中插入节点-LintCode
- LintCode 85. 在二叉查找树中插入节点
- lintcode 85 在二叉树中插入节点
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
- lintcode insert-node-in-a-binary-search-tree 在二叉查找树中插入节点
- LintCode(S)二叉查找树插入节点
- DSP投放进阶指南
- 算法导论 XOR双向循环链表——内存高效链表
- 我也工作了
- HDU 5463 Clarke and minecraft
- C语言算法训练1
- lintcode-在二叉查找树中插入节点-85
- Host "localhost " is not allowed to connect to mysql server解决方法
- 剑指offer - 将字符串中空格替换为20%
- LayoutInflater的inflate函数用法详解
- node.js面试题大全-侧重后端应用与对Node核心的理解
- list
- Project configuration is not up-to-date with pom.xml
- 犀牛——第13章 13.2 在HTML里嵌入JavaScript
- css3之3D盒子 以及css3样式兼容判断