Skip to content

Instantly share code, notes, and snippets.

@fpdjsns
Created March 10, 2019 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fpdjsns/902f39da216af4aa2a344dabe0b3e739 to your computer and use it in GitHub Desktop.
Save fpdjsns/902f39da216af4aa2a344dabe0b3e739 to your computer and use it in GitHub Desktop.
[leetcode] 998. Maximum Binary Tree II : https://leetcode.com/problems/maximum-binary-tree-ii/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* insertIntoMaxTree(TreeNode* root, int val) {
if(root == NULL) return new TreeNode(val);
if(root->val < val){
TreeNode* newNode = new TreeNode(val);
newNode->left = root;
return newNode;
}
root->right = insertIntoMaxTree(root->right, val);
return root;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment