Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree.

Notice

You can assume there is no duplicate values in this tree + node.

 
Example

Given binary search tree as follow, after Insert node 6, the tree should be:

  2             2
/ \ / \
1 4 --> 1 4
/ / \
3 3 6
Challenge

Can you do it without recursion?

解法一:

 public class Solution {
/**
* @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.
*/
public TreeNode insertNode(TreeNode root, TreeNode node) {
if (root == null) {
return node;
}
if (root.val > node.val) {
root.left = insertNode(root.left, node);
} else {
root.right = insertNode(root.right, node);
}
return root;
}
}

解法二:

 public class Solution {
/**
* @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.
*/
public TreeNode insertNode(TreeNode root, TreeNode node) {
if (root == null) {
root = node;
return root;
}
TreeNode tmp = root;
TreeNode last = null;
while (tmp != null) {
last = tmp;
if (tmp.val > node.val) {
tmp = tmp.left;
} else {
tmp = tmp.right;
}
}
if (last != null) {
if (last.val > node.val) {
last.left = node;
} else {
last.right = node;
}
}
return root;
}
}

解法三:

 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) {
stack<TreeNode * > sta;
sta.push(root);
TreeNode * last = NULL;
while (sta.size() != ) {
TreeNode * now = sta.top();
sta.pop();
if (now == NULL) {
if (last == NULL) {
root = node;
} else if (last->val <= node->val) {
last -> right = node;
} else {
last -> left = node;
}
break;
} else if (now->val <= node->val) {
sta.push(now->right);
} else {
sta.push(now->left);
}
last = now;
}
return root;
}
};

参考@DLNU-linglian 的代码

85. Insert Node in a Binary Search Tree【easy】的更多相关文章

  1. Lintcode: Insert Node in a Binary Search Tree

    Given a binary search tree and a new tree node, insert the node into the tree. You should keep the t ...

  2. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  3. 501. Find Mode in Binary Search Tree【LeetCode by java】

    Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...

  4. 543. Diameter of Binary Tree【Easy】【二叉树的直径】

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  5. Lintcode85-Insert Node in a Binary Search Tree-Easy

    85. Insert Node in a Binary Search Tree Given a binary search tree and a new tree node, insert the n ...

  6. [Swift]LeetCode701. 二叉搜索树中的插入操作 | Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  7. [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  8. [LeetCode] 701. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

  9. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ...

随机推荐

  1. as3垃圾回收机制

    as3垃圾回收机制 垃圾回收机制详解 能力越大责任越大,这对actionscript3.0来说一点没错.引入这些新控件带来一个副作用:垃圾收集器不再支持自动为你收集 垃圾等假设.也就是说Flash开发 ...

  2. Linux/Unix C编程之的perror函数,strerror函数,errno

    #include <stdio.h> // void perror(const char *msg); #include <string.h> // char *strerro ...

  3. [Angular] Angular i18n Alternative Expressions Support (select)

    For example we have those code: <div class="course-category" [ngSwitch]="course.ca ...

  4. Node.js abaike图片批量下载Node.js爬虫1.01版

    //====================================================== // abaike图片批量下载Node.js爬虫1.01 // 1.01 修正了输出目 ...

  5. ssm整合(Spring+SpringMVC+Mybatis)

    一.Spring Spring致力于提供一种方法管理你的业务对象.IOC容器,它可以装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用 ...

  6. Loadrunner关于页面检查的几个函数详解

    环境:Loadrunner版本:8.0自建一个test.html文件:<html><head><meta name="google1" content ...

  7. js实现回放拖拽轨迹-------Day48

    今天有点小高兴,csdn博客浏览量过万了,在过去还从来没有过这么高的浏览量呢.不得不说.太多时候还是有些矫情.可看到这些鼓舞还是忍不住高兴啊,至少,这样让我有一种行内人员的感觉,吾道不孤啊. 闲话不多 ...

  8. UIKit class hierarchy

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY3puY2Rtcw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...

  9. ASP.Net MVC3/4中Model验证错误信息的本地化

    最近使用ASP.Net MVC4做一个B/S的管理系统,里面有N多的Action和View Model,View Model上又有N多的验证. 一开始写的时候虽然知道要实现多语言,但是没有过多考虑,本 ...

  10. 解决 TextMate 2 无法安装 Emmet 插件

    本篇文章由:http://xinpure.com/solving-textmate-2-cannot-install-emmet-plugin/ 前端神器 Emmet 插件原名为 ZedCoding ...