タイトルアドレス:
https://www.lintcode.com/problem/insert-node-in-a-binary-search-tree/description
BSTを指定すると、ノードをノードに挿入し、挿入後にツリーのルートを返します。コードは次のとおりです。
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 (node.val < root.val) {
root.left = insertNode(root.left, node);
} else if (node.val > root.val) {
root.right = insertNode(root.right, node);
}
return root;
}
}
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int x) {
val = x;
}
}
時空の複雑さ 。