Advertisement
sweet1cris

Untitled

Jan 9th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. /**
  2.  * Definition of TreeNode:
  3.  * public class TreeNode {
  4.  *     public int val;
  5.  *     public TreeNode left, right;
  6.  *     public TreeNode(int val) {
  7.  *         this.val = val;
  8.  *         this.left = this.right = null;
  9.  *     }
  10.  * }
  11.  */
  12. public class Solution {
  13.     /**
  14.      * @param root: The root of the binary search tree.
  15.      * @param node: insert this node into the binary search tree
  16.      * @return: The root of the new binary search tree.
  17.      */
  18.     public TreeNode insertNode(TreeNode root, TreeNode node) {
  19.         if (root == null) {
  20.             return node;
  21.         }
  22.         if (root.val > node.val) {
  23.             root.left = insertNode(root.left, node);
  24.         } else {
  25.             root.right = insertNode(root.right, node);
  26.         }
  27.         return root;
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement