Leetcode Problem 998. Maximum Binary Tree II

998. Maximum Binary Tree II

Leetcode Solutions

Iterative Right Subtree Traversal

  1. Create a dummy node with value greater than any possible node value and set its right child to the root.
  2. Initialize a pointer parent to the dummy node.
  3. While parent has a right child and the value of the right child is greater than val, move parent to its right child.
  4. Create a new node with val as its value.
  5. Set the new node's left child to parent's right child.
  6. Set parent's right child to the new node.
  7. Return the right child of the dummy node, which is the new root of the modified tree.
UML Thumbnail

Reconstruct and Rebuild Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...