Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- func increasingBST(_ root: TreeNode?) -> TreeNode? {
- //increasingOST : increasing order search tree.
- var increasingOST: TreeNode? = TreeNode(-1)
- var current_node = increasingOST
- //inner method
- func inorderTraversal(_ root: TreeNode?) {
- guard let root = root else {
- return
- }
- // traverse the left subtree
- inorderTraversal(root.left)
- //create a new node using current root node val
- // assign as a right child to current node of\
- // the increasing order search tree..
- current_node?.right = TreeNode(root.val)
- //make it looking-up for the next right node.
- current_node = current_node!.right
- // traverse the right subtree
- inorderTraversal(root.right)
- }
- inorderTraversal(root)
- return increasingOST?.right
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement