Lintcode 85. 在二叉查找树中插入节点

《Lintcode 85. 在二叉查找树中插入节点》

——————————————–

 

AC代码:

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

 

 

题目来源: http://www.lintcode.com/zh-cn/problem/insert-node-in-a-binary-search-tree/

 

    原文作者:CC11001100
    原文地址: https://www.cnblogs.com/cc11001100/p/6243518.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞