Comment on page

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.

代码

Approach #1 Recursion

public class Solution {
public TreeNode insertNode(TreeNode root, TreeNode node) {
if (root == null) return node;
​
if (root.val > node.val) {
root.left = insertNode(root.left, node);
} else {
root.right = insertNode(root.right, node);
}
​
return root;
}
​
}
Last modified 3yr ago