LF.51.Insert In Binary Search Tree

Insert a key in a binary search tree if the binary search tree does not already contain the key. Return the root of the binary search tree.

Assumptions

There are no duplicate keys in the binary search tree

If the key is already existed in the binary search tree, you do not need to do anything

Examples

        5

      /
    3        8

  /
 1     4

insert 11, the tree becomes

        5

      /
    3        8

  /   \
 1     4       11

insert 6, the tree becomes

        5

      /
    3        8

  /   \     /
 1     4   6    11

DFS recursive:

 1 public class Solution {
 2   public TreeNode insert(TreeNode root, int key) {
 3     // Write your solution here
 4     //this is the base case
 5     if (root == null ) {
 6         return new TreeNode(key) ;
 7     }
 8     if (root.key == key){
 9         return root ;
10     }
11     if (root.key > key) {
12         //go to left
13         root.left = insert(root.left, key) ;
14     }
15     if (root.key < key ) {
16         //go to right
17         root.right = insert(root.right, key) ;
18     }
19     //back -> upper -> upper upper to the top then return
20     return root ;
21   }
22 }

since this is called tail recursion, you are recommended to do it in interative: time: O(n) space: O(1)

Interative:

原文地址:https://www.cnblogs.com/davidnyc/p/8655269.html

时间: 2024-11-09 00:02:11

LF.51.Insert In Binary Search Tree的相关文章

LF.53.Delete In Binary Search Tree

Delete the target key K in the given binary search tree if the binary search tree contains K. Return the root of the binary search tree. Find your own way to delete the node from the binary search tree, after deletion the binary search tree's propert

PTA Build A Binary Search Tree(C语言)

题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys gre

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

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

[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

[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

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

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