Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. TreeNode newNode = null;
  12. public TreeNode insertIntoMaxTree(TreeNode root, int val) {
  13. newNode = new TreeNode(val);
  14. return helper(root);
  15. }
  16. public TreeNode helper(TreeNode root)
  17. {
  18. if(root == null)
  19. return null;
  20.  
  21. if(root.val < newNode.val)
  22. {
  23. newNode.left = root;
  24. return newNode;
  25. }
  26. if(root.right != null)
  27. root.right = helper(root.right);
  28. else
  29. root.right = newNode;
  30. return root;
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement