|
68 | 68 |
|
69 | 69 | <p> </p>
|
70 | 70 |
|
71 |
| - |
72 | 71 | ## 解法
|
73 | 72 |
|
74 | 73 | <!-- 这里可写通用的实现逻辑 -->
|
75 | 74 |
|
| 75 | +已知最大树 A,插入一个值 val 后,返回插入后的树。 |
| 76 | + |
| 77 | +如果 val 是最大数,那么将 val 作为新的根节点,root 作为新的根节点的左子树。 |
| 78 | + |
| 79 | +如果 val 不是最大数,由于 val 是在最后追加的数,那么一定是在 root 的右边,所以将 val 作为新节点插入 root 的右子树即可。 |
| 80 | + |
76 | 81 | <!-- tabs:start -->
|
77 | 82 |
|
78 | 83 | ### **Python3**
|
79 | 84 |
|
80 | 85 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
81 | 86 |
|
82 | 87 | ```python
|
83 |
| - |
| 88 | +# Definition for a binary tree node. |
| 89 | +# class TreeNode: |
| 90 | +# def __init__(self, val=0, left=None, right=None): |
| 91 | +# self.val = val |
| 92 | +# self.left = left |
| 93 | +# self.right = right |
| 94 | +class Solution: |
| 95 | + def insertIntoMaxTree(self, root: TreeNode, val: int) -> TreeNode: |
| 96 | + if root is None or root.val < val: |
| 97 | + return TreeNode(val, root, None) |
| 98 | + root.right = self.insertIntoMaxTree(root.right, val) |
| 99 | + return root |
84 | 100 | ```
|
85 | 101 |
|
86 | 102 | ### **Java**
|
87 | 103 |
|
88 | 104 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
89 | 105 |
|
90 | 106 | ```java
|
| 107 | +/** |
| 108 | + * Definition for a binary tree node. |
| 109 | + * public class TreeNode { |
| 110 | + * int val; |
| 111 | + * TreeNode left; |
| 112 | + * TreeNode right; |
| 113 | + * TreeNode() {} |
| 114 | + * TreeNode(int val) { this.val = val; } |
| 115 | + * TreeNode(int val, TreeNode left, TreeNode right) { |
| 116 | + * this.val = val; |
| 117 | + * this.left = left; |
| 118 | + * this.right = right; |
| 119 | + * } |
| 120 | + * } |
| 121 | + */ |
| 122 | +class Solution { |
| 123 | + public TreeNode insertIntoMaxTree(TreeNode root, int val) { |
| 124 | + if (root == null || root.val < val) { |
| 125 | + return new TreeNode(val, root, null); |
| 126 | + } |
| 127 | + root.right = insertIntoMaxTree(root.right, val); |
| 128 | + return root; |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +### **C++** |
| 134 | + |
| 135 | +```cpp |
| 136 | +/** |
| 137 | + * Definition for a binary tree node. |
| 138 | + * struct TreeNode { |
| 139 | + * int val; |
| 140 | + * TreeNode *left; |
| 141 | + * TreeNode *right; |
| 142 | + * TreeNode() : val(0), left(nullptr), right(nullptr) {} |
| 143 | + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} |
| 144 | + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} |
| 145 | + * }; |
| 146 | + */ |
| 147 | +class Solution { |
| 148 | +public: |
| 149 | + TreeNode* insertIntoMaxTree(TreeNode* root, int val) { |
| 150 | + if (root == nullptr || root->val < val) { |
| 151 | + return new TreeNode(val, root, nullptr); |
| 152 | + } |
| 153 | + root->right = insertIntoMaxTree(root->right, val); |
| 154 | + return root; |
| 155 | + } |
| 156 | +}; |
| 157 | +``` |
91 | 158 |
|
| 159 | +### **Go** |
| 160 | +
|
| 161 | +```go |
| 162 | +/** |
| 163 | + * Definition for a binary tree node. |
| 164 | + * type TreeNode struct { |
| 165 | + * Val int |
| 166 | + * Left *TreeNode |
| 167 | + * Right *TreeNode |
| 168 | + * } |
| 169 | + */ |
| 170 | +func insertIntoMaxTree(root *TreeNode, val int) *TreeNode { |
| 171 | + if root == nil || root.Val < val { |
| 172 | + return &TreeNode{ |
| 173 | + Val: val, |
| 174 | + Left: root, |
| 175 | + Right: nil, |
| 176 | + } |
| 177 | + } |
| 178 | + root.Right = insertIntoMaxTree(root.Right, val) |
| 179 | + return root |
| 180 | +} |
92 | 181 | ```
|
93 | 182 |
|
94 | 183 | ### **...**
|
|
0 commit comments