A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 998. Maximum Binary Tree II, the solution in this repository is mainly a two pointers solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers, sliding window.
The notes already sitting in the source point us in the right direction:
- https://leetcode.com/problems/maximum-binary-tree-ii/discuss/242936/JavaC%2B%2BPython-Recursion-and-Iteration
- Solution 1: Recursion
- time: O(n), space: O(n)
Guide
When?
Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are insertIntoMaxTree.
Guide
Why?
The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//https://leetcode.com/problems/maximum-binary-tree-ii/discuss/242936/JavaC%2B%2BPython-Recursion-and-Iteration
02//Solution 1: Recursion
03//time: O(n), space: O(n)
04//Runtime: 4 ms, faster than 89.46% of C++ online submissions for Maximum Binary Tree II.
05//Memory Usage: 13.1 MB, less than 20.00% of C++ online submissions for Maximum Binary Tree II.
06
07/**
08 * Definition for a binary tree node.
09 * struct TreeNode {
10 * int val;
11 * TreeNode *left;
12 * TreeNode *right;
13 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
14 * };
15 */
16class Solution {
17public:
18 TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
19 if(root && root->val > val){
20 //operate on its right subtree and then return
21 root->right = insertIntoMaxTree(root->right, val);
22 return root;
23 }
24 //null node or root->val <= val
25 TreeNode* node = new TreeNode(val);
26 node->left = root;
27 return node;
28 }
29};
30
31//Solution 2: Iteration
32//Runtime: 4 ms, faster than 89.46% of C++ online submissions for Maximum Binary Tree II.
33//Memory Usage: 13.5 MB, less than 20.00% of C++ online submissions for Maximum Binary Tree II.
34//time: O(n), space: O(1)
35/**
36 * Definition for a binary tree node.
37 * struct TreeNode {
38 * int val;
39 * TreeNode *left;
40 * TreeNode *right;
41 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
42 * };
43 */
44class Solution {
45public:
46 TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
47 TreeNode *node = new TreeNode(val), *cur = root;
48 if(root->val < val){
49 node->left = root;
50 return node;
51 }
52 while(cur->right && cur->right->val > val){
53 cur = cur->right;
54 }
55 node->left = cur->right;
56 cur->right = node;
57 return root;
58 }
59};
Cost