Advertisement
halaluddin

Day17-Apr-Increasing_OST

Apr 17th, 2022
1,589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.02 KB | None | 0 0
  1. class Solution {
  2.    
  3.     func increasingBST(_ root: TreeNode?) -> TreeNode? {
  4.        
  5.         // increasingOST : increasing order search tree.
  6.         var increasingOST: TreeNode? = TreeNode(-1)
  7.         var current_node = increasingOST
  8.        
  9.         // inner method
  10.         func inorderTraversal(_ root: TreeNode?) {
  11.             guard let root = root else {
  12.                 return
  13.             }
  14.             // traverse the left subtree
  15.             inorderTraversal(root.left)  
  16.  
  17.             // create a new node using current root node val
  18.             // assign as a right child to current node of\
  19.             // the increasing order search tree..
  20.             current_node?.right = TreeNode(root.val)
  21.             //make it looking-up for the next right node.
  22.             current_node = current_node!.right
  23.            
  24.             // traverse the right subtree
  25.             inorderTraversal(root.right)
  26.         }
  27.        
  28.         inorderTraversal(root)
  29.        
  30.         return increasingOST?.right
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement