[Lintcode] Insert Node in a Binary Search Tree

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 tree still be a valid binary search tree.

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?

SOLUTION 1:

首先从最好理解的recursion开始。

递归算法,1,首先这个输入跟我要做递归的函数input相同,所以没必要再做一个辅助函数,2,然后,再看递归具体步骤,从局部考虑,如果node.val > root.val 递归应该向右边进行(也就是说node一定会加在root右子树上),同理node.val < root.val ==>递归向左进行。3,最后考虑递归的结束条件是什么,这题的结束条件就是指针移动到一个没有左儿子或者右儿子的地方,把node加进去,返回这个结果 ==> 也就是root.left/right = node。

具体看代码:

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
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);
        }
        if (root.val < node.val){
            root.right = insertNode(root.right, node);
        }
        return root;
    }
}

SOLUTION 2:

非递归方法:

非递归就是说,找到root点,root左子树或者右子树是空的,选择性的把node放在上面。

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
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 (node == null){
            return root;
        }
        TreeNode current = root;
        while (current != null){
            if (node.val > current.val && current.right == null){
                current.right = node;
                break;
            } else if (node.val < current.val && current.left == null){
                current.left = node;
                break;
            } else if (node.val > current.val){
                current = current.right;
            } else {
                current = current.left;
            }
        }
        return root;
    }
}

时间: 2024-11-04 14:33:58

[Lintcode] Insert Node in a Binary Search Tree的相关文章

[lintcode easy]Insert Node in a Binary Search Tree

Insert Node in a Binary Search Tree 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? /** * Definition of TreeNode: * public class TreeNod

lintcode 容易题:Insert Node in a Binary Search Tree 在二叉查找树中插入节点

题目:  在二叉查找树中插入节点 给定一棵二叉查找树和一个新的树节点,将节点插入到树中. 你需要保证该树仍然是一棵二叉查找树.  样例 给出如下一棵二叉查找树,在插入节点6之后这棵二叉查找树可以是这样的: 挑战 能否不使用递归? 解题: 递归的方法比较简单 Java程序: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public

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 tree still be a valid binary search tree. Example Given binary search tree as follow: / 4 / after Insert node 6, the tree should be: / 4 / \ 6 Challeng

LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height

C++ /** * 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 A: A sorted (increasi

lintcode 中等题:unique Binary Search Tree 不同的二叉查找树

题目 不同的二叉查找树 给出 n,问由 1...n 为节点组成的不同的二叉查找树有多少种? 样例 给出n = 3,有5种不同形态的二叉查找树: 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 解题 public class Solution { /** * @paramn n: An integer * @return: An integer */ public int numTrees(int n) { // write your code here

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 the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. No

[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 the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST. No

【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 the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original B

Binary search tree system and method

A binary search tree is provided for efficiently organizing values for a set of items, even when values are duplicated. In generating the binary search tree, the value of each item in a set of values is determined. If a particular value is unique and