lintcode-easy-Insert Node in a Binary Search Tree

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-easy-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?

 

两种方法都写一下吧,递归和非递归

1. 递归

/**
 * 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) {
        // write your code here
        if(root == null)
            return node;
        
        if(root.val == node.val){
            return root;
        }
        
        if(root.left == null && root.val > node.val){
            root.left = node;
            return root;
        }
        
        if(root.right == null && root.val < node.val){
            root.right = node;
            return root;
        }
        
        if(root.val > node.val){
            TreeNode left = insertNode(root.left, node);
            root.left = left;
        }
        else if(root.val < node.val){
            TreeNode right = insertNode(root.right, node);
            root.right = right;
        }
        
        return root;
    }
}

2. 非递归

/**
 * 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) {
        // write your code here
        if(root == null)
            return node;
        
        if(root.val == node.val)
            return root;
        
        TreeNode p = root;
        
        while(!(p.left == null && p.val > node.val) && !(p.right == null && p.val < node.val)){
            if(p.val > node.val)
                p = p.left;
            else if(p.val == node.val)
                return root;
            else
                p = p.right;
        }
        if(p.left == null && p.val > node.val){
            p.left = node;
            return root;
        }
        else{
            p.right = node;
            return root;
        }
    }
}

 

以上是关于lintcode-easy-Insert Node in a Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章

java中,Node node = (Node) value; 其中Node是接口,这个语句是啥意思啊?谢谢。

如何执行 node

关于node,node-sass,sass-loader版本

Node.js 入门

Node学习—运行node服务demo

10.DOM