Created
March 10, 2019 02:35
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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