lintcode-85-在二叉查找树中插入节点
2017-07-09 16:46
330 查看
85-在二叉查找树中插入节点
给定一棵二叉查找树和一个新的树节点,将节点插入到树中。
你需要保证该树仍然是一棵二叉查找树。
注意事项
You can assume there is no duplicate values in this tree + node.样例
给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的:挑战
能否不使用递归?标签
二叉查找树 LintCode 版权所有code
/** * 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 TreeNode* curNode = root, *curNodeNext = root; if(root == NULL) { root = node; } while(curNodeNext != NULL) { if(curNodeNext->val > node->val) { curNode = curNodeNext; curNodeNext = curNodeNext->left; if(curNode->left == NULL) { curNode->left = node; break; } } else if(curNodeNext->val < node->val) { curNode = curNodeNext; curNodeNext = curNodeNext->right; if(curNode->right == NULL) { curNode->right = node; break; } } } return root; } };
相关文章推荐
- lintcode-在二叉查找树中插入节点-85
- LintCode(85)在二叉查找树中插入节点
- LintCode 在二叉查找树中插入节点
- LintCode 在二叉查找树中插入节点
- LintCode 85-在二叉查找树中插入节点
- 在二叉查找树中插入节点-LintCode
- 85 - 在二叉查找树中插入节点
- LintCode 85. 在二叉查找树中插入节点
- 【LintCode 简单】85. 在二叉查找树中插入节点
- lintcode ----在二叉查找树中插入节点
- Lintcode 85. 在二叉查找树中插入节点
- LintCode-在二叉查找树中插入节点
- LintCode:在二叉查找树中插入节点
- lintcode 在二叉查找树中插入节点
- 在二叉查找树中插入节点
- 二叉查找树中节点的包含,插入,删除操作
- 在二叉查找树中插入节点
- 删除二叉查找树的节点-LintCode
- C++模板实现二叉查找树(一 树的数据结构定义与节点插入)
- 算法-二叉查找树-插入节点